fractal_7 fractal_7 - 2 months ago 7
C# Question

How to display a message everytime a single cell is selected in a datagridview?

I have four datagridviews and one text box and i want to display a different message for every cell the user might select in one of the dgvs. Any idea is welcome!!!

Answer

I prefer to specify most event handlers in code as below, as opposed to using the GUI which puts the code in the form.designer.cs but that is personal preference

public Form1()
 { 
      dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(populateTextBox());
      dataGridView2.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(populateTextBox());
      dataGridView3.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(populateTextBox());
      dataGridView4.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(populateTextBox());
 }

private void populateTextBox(object sender, DataGridViewCellEventArgs e)
{
    //code here
    //You can use e.Value (cell value data type dependant) if required
}
Comments