How to get the ID of the current DGV row

  • Thread starter Thread starter dbuchanan
  • Start date Start date
D

dbuchanan

Hello,

How to I get the primary key (ID) of the currently selected row? I need to
open a child form based on that ID.

Thank you.
 
Hi Dbuchanan,

I assume you mean that you wanted to get the primary key cell value in
current DataGridView selected row, yes? If so, you may use
DataGridView.CurrentRow.DataBoundItem to get the DataRowView of the
underlying mapping object, then you may retrieve its primary key cell
value. Sample is listed below:

DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));

dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr);
}
this.dataGridView1.DataSource = dt;
}

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataRowView drv = this.dataGridView1.CurrentRow.DataBoundItem as
DataRowView;
if (drv != null)
{
MessageBox.Show(drv[dt.PrimaryKey[0].ColumnName].ToString());
}
}
Hope this helps.

Best regards,
Jeffrey Tan
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.
 
Jeffrey,

Thank you very much for your help.

This stuff is not very intuitive to me. More on this in a moment.
I am using a XSD with a TableAdapter and a BindingSource so I had to change
it up a little. I was able to arrive at the same thing by this code;

private void btnAddViewEditTasks_Click(object sender, EventArgs e)
{
DataRowView drv = this.dgvMTasks.CurrentRow.DataBoundItem as
DataRowView;
if (drv != null)
{
MessageBox.Show(drv.Row[0].ToString());
}
}

The syntax "drv.Row[0].ToString()" seems very counter-intuitve to me because
it looks like I am asking for the 0th row. I would have expected something
like "drv.Column[0]" and that is one of the reasons I could not find the
correct syntax on my own.

Any comments on why it is row[0] instead of column[0]? It seams inconsitent,
not following a consistent pattern.

....
I find it very hard to discover the syntax I need. Does it get any easier?
Any suggestions for learning this when you do not even know where to look in
the help - especially whe the help filter still gives so much unwanted
results (like FoxPro) even when a filter is set for C#. It is so very #$%&
frustrating. Any suggetsions?

Thanks again,
Doug.
 
Hi Doug,

Thanks for your feedback.

Ok, let me explain the syntax.

First, this.dgvMTasks.CurrentRow.DataBoundItem already gives you the
current selected row in the DataView. This is DataRowView.

Since drv is of type DataRowView, drv.Row means DataRowView.Row property.
As documented in the link below, the DataRowView.Row property returns the
DataRow reference being viewed:
http://msdn2.microsoft.com/en-us/library/system.data.datarowview.row.aspx

Now, drv.Row[0] actually means using the indexer property of DataRow, so it
equals to DataRow.Item[0]. The parameter "0" is the column index into the
DataRow. So you are getting the first column item data in the current row,
not getting the first row. Below is the offical link for this indexer
property:
http://msdn2.microsoft.com/en-us/library/kwfe1t7z.aspx

Regarding how to find out the syntax, you may follow my logic analysis
above and examine various class properties types in MSDN. This is the
correct way to get it out.

Hope this is clear. Thanks.

Best regards,
Jeffrey Tan
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.
 
Back
Top