update some of the selected records

CGK

Member
Joined
Jul 31, 2007
Messages
7
Programming Experience
Beginner
Hi All!
I'm new to VB.NET coding! Please guide me with the following!


I have a datagrid populated with values from database table. Now, I need to update some of the selected records. For this, I need to Identify the records selected by the user and extract the first column (Primary key of db table) of it.

May be I need to convert the selected record collection into an array of strings.

Can anyone guide me how I can do this ?

Any suggestions/pointers would be very helpful!

TIA.

CGK.
 
Last edited:
Are you really using a DataGrid or is it actually a DataGridView? Please be careful to use the correct names as they are not the same thing. If you are using a DataGrid then I suggest you switch unless there's a specific reason not to.
 
I'm using DataGridViews!

Ya! You are right.

The DataGridView.SelectedRows property retrieves the selected record collection.
But I need to access each of these records so that I can extract the first column (Primary Key) for updating the database table. I'm not able to find how.

Can you help me in this regard ?

TIA.
CGK.
 
Here's how you do it:

Press F2
Type SelectedRows
Read about it
See that SelectedRows returns an collection of type DataGridViewSelectedRowCollection
Click on it (the word: DataGridViewSelectedRowCollection)
Look at the members
See it has Item() which returns a DataGridViewRow, or it has enumeration..
Click on Item()
Click on DataGridViewRow word and look at the members of a DataGridViewRow
We can see DataBoundItem, or Cells() collection.. DataBOundItem probalby gives us our DataRow, Cells() is a collection of the cells on show.
Choose which to access (i usually go for DataRow, rather than cells but cells is easier).. if going for the DataBoundItem, you'll need to cast it to a DataRow or typed DataRow, then use it.. or use the Cells() with an indexer..


As you can see.. I dont remember this stuff; i dont know the answer to every question I'm asked here.. but I know how to find the answer. Hopefully me writing this for you, will help you use the Object Browser and MSDN etc to find your answers :)

So you kinda infer that:

VB.NET:
ForEach dgvr as DataGridViewRow in myDGV.SelectedRows 'foreach row in rowcollection ENUMERATION
  'dgvr is set to each row in turn. kinda like saying
  'for i = 0 to dgv.selectedrows.count, dgvr = dgv.selectedrows(i)
  'only, better..

  MessageBox.Show(DirectCast(dgvr.DataBoundItem, MyTypedDataRow).MY_COLUMN_TO_SHOW)

Next dgvr

Or something like that :)
 
Back
Top