• 0

[C#] WinForms DataGrid Control


Question

I'm just going to explain what i want to do.

I have a DataGrid Control on one of my Forms, its populated via an SQLite database which works great. Now when a user double clicks on one of the rows, i want to open another form that shows details about the row the user doubleclicked on, i have a primary key (field name is 'id') in my database table, so if i could just pass along the ID of the row along to the new form then i can use that to query the database in the new form to do what i need there.

Any help would be great.

Thanks!

Michael

Link to comment
https://www.neowin.net/forum/topic/727788-c-winforms-datagrid-control/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

How are you binding data to the grid? Are you using a BindingSource? .DataBind()? in a loop?

You could either have a column that stores the id, or you could have it on the row/cells Tag property, either way, you can then access that particular value through a double click event:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
	int id = (int)dataGridView1.Rows[e.RowIndex].Tag;
	// or int id = (int)dataGridView1.Rows[e.RowIndex].Cells["NameOfIdColumn"];

	// do something here
}

Just make sure you set those values accordingly.

  • 0
  Antaris said:
How are you binding data to the grid? Are you using a BindingSource? .DataBind()? in a loop?

You could either have a column that stores the id, or you could have it on the row/cells Tag property, either way, you can then access that particular value through a double click event:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
	int id = (int)dataGridView1.Rows[e.RowIndex].Tag;
	// or int id = (int)dataGridView1.Rows[e.RowIndex].Cells["NameOfIdColumn"];

	// do something here
}

Just make sure you set those values accordingly.

That looks great. I don't have internet access at home till Monday so I can't show you any code since its on my home computer. I'm able to pass things from one form to another it was just getting the value of a cell of the row selected which the code you provided should do.

Thanks,

Michael

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.