datagrid

J

John

I am having a hard time getting the value of the primary key of the selected
row in a datagrid. The datagrid is just displaying a dataset. Can someone
ease my pain?
 
L

Linda Liu [MSFT]

Hi John,

Do you mean to get the value of the primary key of the current row in a
DataGrid?

I think you could do this in two steps. The first step is to get the
current row in the DataGrid. The second step is to get the value of the
primary key of this row.

Step 1:

You could make use of the Current property of CurrencyManager object that
are associated with the dataset to get the current row. You could use the
form's BindingContext property to get the CurrencyManger object.

Below is a sample code.

// this refers to the form that contains the dataset
DataRowView row =
(DataRowView)this.BindingContext[this.dataset11,"City"].Current;

Step 2:

You could make use of the PrimaryKey property of the DataTable to get the
array of columns that function as the primary keys of the DataTable. If
there's only one primary key in the DataTable, you could access it with
index 0.

Below is a sample code.

object keyvalue = row[this.dataset1.tableName.PrimaryKey[0].ColumnName];

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

John

"Do you mean to get the value of the primary key of the current row in a
DataGrid?" Yes, my goal is the user can select one or possibly more items in
the data grid and the I return the ID of the item(s) selected to the
program. I would think it's an easy task but I haven't found it yet.

I don't understand your code, I am using VB is that why?
 
G

Guest

Heres what I do (it took me a while to figure out how to get a reference to
the DataView and/or DataRowView that the grid is bound to, when I am
explicitly binding the grid using something like
grdMyGrid.SetDataBinding(dsMyDataSet, "MyTableName")):
Note that this is for framework 1.1...

'First get the reference to the DataRowView
Dim dvMyRowView As DataRowView = grdMyGrid.BindingContext(dsMyDataSet,
"MyTableName").Current

'Now we have the DataRowView for our current selected grid row.
'Simply access the column you want using the PK column mapping name or the
row index.
Dim strValue As Object = dvMyRowView("MyPK")
Dim strValue2 As Object = dvMyRowView(0)

Hope that works. Code may not be perfect, but I'm pretty sure its close.
 
J

John

It sure seems close I agree. The only problem I'm haveing is, it doesn't
matter what row I select, I only seem to get data from the first row. Any
idea why?
 
G

Guest

Try this:

- Get the DataView that the RowDataView belongs to (RowDataView.DataView)
- DataView(Grid.CurrentRowIndex)("PKName") should have your value :)
 
L

Linda Liu [MSFT]

Hi John,

The code I provided in my first reply is used to get the value of the
primary key of the current row of a DataGrid, not of the selected row. And
the VB.Net version of my sample code is below.

// Option 1 to get the current row
Dim row As DataRowView = CType(Me.BindingContext(Me.dataset11,
"City").Current, DataRowView)
Dim keyvalue As Object = row(Me.dataset11.City.PrimaryKey(0).ColumnName)

In the above sample, I make use of the CurrencyManager object associated
with the dataset11 to get the current row. I could always get the correct
current row by option 1 even after I sort the DataGrid, i.e by clicking on
the the DataGrid's column headers.

In fact, I could also use the CurrentRowIndex property of the DataGrid to
get the current row. The following is a sample.

// Option 2 to get the current row
Dim row As DataRow = Me.dataset11.City.Rows(Me.DataGrid1.CurrentRowIndex)

However, if I sort the DataGrid, the indexes of the records in the DataGrid
aren't consistent to that in the DataTable. Thus, in this case, I couldn't
get the correct current row by option 2.
The only problem I'm haveing is, it doesn't matter what row I select, I
only seem to get data from the first row. Any idea why?

What I want to point out is that the selected row of a DataGrid is not
equal to the current row. You could select several rows in a DataGrid,
however the current row is the only one indicated by an arrow in the row
header.

Unfortunately, we could not get the selected rows of a DataGrid directly
because there isn't such a property or method of DataGrid to return the
selected rows. The CurrencyManager object doesn't provide such a function
either. But we could call the IsSelected method of DataGrid to determin
which rows are selected. We could store the indexes of the selected rows
into a collection and then get the value of the primary key of each row
indicated in the collection. Below is a sample.

Dim selectedRows As New ArrayList
Dim keyValues as New ArrayList

// firstly, get the index of all the selected rows in the DataGrid into
selectedRows
For i As Integer = 0 To Me.dataset11.City.Rows.Count - 1
If Me.DataGrid1.IsSelected(i) Then
selectedRows.Add(i)
End If
Next

// secondly, get the value of the primary key of each row indicated in the
collection and store it into another collection
For i As Integer = 0 To selectedRows.Count - 1
keyValues.Add(Me.dataset11.City.Item(CType(selectedRows(i),
Int32)).Item(Me.dataset11.City.PrimaryKey(0).ColumnName))
Next

This method has a limitation. If you sort the DataGrid, the indexes of the
records in the DataGrid aren't consistent to that in the DataTable. So we
couldn't get the correct selected rows from the DataTable according to the
collection. I think you may set the AllowSorting property of the DataGrid
to false.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
J

John

i get the same results as from before, I print out the value but it is
always the value of the first row, never the "current" row. I select some
field in the Datgrid so the arrow is on that row, then I click my command
button that runs the code you gave me and msgbox the value, but it is alway
the value of the id of the first row. Please explain why?
 
L

Linda Liu [MSFT]

Hi John,

It seems strange that you always get the value of the ID field of the first
row running the code I gave you.

Would you please show me the code that you write in the command button's
Click event handler?

I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top