Sometimes you may want to pass information about the row or cell after double clicking a data grid. This simple code shows how its done when the data grid is using a dataset. When double clicking a cell you need to first setup the event handler. You simply enter this in your constructor or class load method:
//double cell click event. dataGrid1 is the name of your grid
this.dataGrid1.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGrid1_CellContentDoubleClick);
this.dataGrid1.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGrid1_CellContentDoubleClick);
Followed by:
private void dataGrid1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//create a new datarow from the dataset and current row slected from datagrid1
DataRow myDatarow = myDataset.Tables["Customer"].Rows[dataGrid1.CurrentRow.Index];
{
//create a new datarow from the dataset and current row slected from datagrid1
DataRow myDatarow = myDataset.Tables["Customer"].Rows[dataGrid1.CurrentRow.Index];
// fill text box with column 0
textBox1.Text = myDatarow[0].ToString();
//fill text box with selected cell value
textBox2.Text = dataGrid1.CurrentCell.Value.ToString();
//you can send the row to a new form if you change or overload the constructor
NewForm1 newForm = new NewForm1(myDatarow);
}
Leave a Reply
You must be logged in to post a comment.