47

I have a DataGridView (Selectionmode: FullRowSelect) and some textboxes on a Windows Form. I want the contents of a row that is clicked (or double clicked) to be displayed in the textboxes.

I tried out this code:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show("Cell Double_Click event calls");
    int rowIndex = e.RowIndex;
    DataGridViewRow row = dataGridView1.Rows[rowIndex];
    textBox5.Text = row.Cells[1].Value;
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int rowIndex = e.RowIndex;
    DataGridViewRow row = dataGridView1.Rows[rowIndex];
    textBox5.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();// row.Cells[1].Value;
}

There are many other textboxes, but the main problem is that none of the events seem to be triggered. What event should I use to do so, or is there some property of the DataGridView that I might have set wrong?

jordanz
  • 367
  • 4
  • 12
Samy S.Rathore
  • 1,825
  • 3
  • 26
  • 43
  • 1
    Go to the form designer and check the Properties/Events of the datgagridview. See here these events are bind to the gridview – Talha Jun 29 '12 at 11:29
  • well i did checked under the yellow lightening bold icon, and these events are listed there, and have the code that i mentioned above... – Samy S.Rathore Jun 29 '12 at 11:33
  • 1
    Strange! put breakpoint and debug your code – Talha Jun 29 '12 at 11:41
  • 2
    Check that your `DataGridView` has its `Enabled` property to `True` – Luis Quijada Jun 29 '12 at 11:45
  • @user1479153 nothing is really obvious when you cannot see directly the code and the problem you try to solve. Don't forget that we're looking forward to help you. – Luis Quijada Jun 29 '12 at 12:50
  • sorry if i offended u, did'nt mean to.....well i did carried out a bit of thorough debugging, thare seems to be something wrong with my pc..i changed a few controls on my form, but they are not reflected whle i run the program..... – Samy S.Rathore Jun 29 '12 at 13:24

6 Answers6

69

You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:

private void dataGridView_SelectionChanged(object sender, EventArgs e) 
{
    foreach (DataGridViewRow row in dataGridView.SelectedRows) 
    {
        string value1 = row.Cells[0].Value.ToString();
        string value2 = row.Cells[1].Value.ToString();
        //...
    }
}

You can also walk through the column collection instead of typing indexes...

Tassisto
  • 9,877
  • 28
  • 100
  • 157
Vale
  • 3,258
  • 2
  • 27
  • 43
  • But in my case The DataGridView should displays only one column i.e StudentName. But when I select the record on the DataGridView I should get all the details to display using other output controls. like name, RollNo, Class, Address etc.. but in the database table contain all the details. Help me how can I get this ? – Sanjeev4evr Apr 09 '14 at 14:05
22

You can try this click event

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        Eid_txt.Text = row.Cells["Employee ID"].Value.ToString();
        Name_txt.Text = row.Cells["First Name"].Value.ToString();
        Surname_txt.Text = row.Cells["Last Name"].Value.ToString();
Tassisto
  • 9,877
  • 28
  • 100
  • 157
helper
  • 221
  • 2
  • 2
  • Just want to point out, that if you are manually\dynamically creating the GridView columns you need to set the "Name" property of the column for this method to work. – cmartin Nov 23 '20 at 18:40
10

First take a label. set its visibility to false, then on the DataGridView_CellClick event write this

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    label.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();
    // then perform your select statement according to that label.
}
//try it it might work for you
hoaz
  • 9,883
  • 4
  • 42
  • 53
vikscool
  • 101
  • 1
  • 2
4

You should check your designer file. Open Form1.Designer.cs and find this line:

windows Form Designer Generated Code.  

Expand this and you will see a lot of code. So check Whether this line is there inside datagridview1 controls if not place it.

this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); 

I hope it helps.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Giri
  • 931
  • 11
  • 31
  • 51
3

Simple solution would be as below. This is improvement of solution from vale.

private void dgMapTable_SelectionChanged(object sender, EventArgs e) 
{
    int active_map=0;
    if(dgMapTable.SelectedRows.Count>0)
        active_map = dgMapTable.SelectedRows[0].Index;
    // User code if required Process_ROW(active_map);
}

Note for other reader, for above code to work FullRowSelect selection mode for datagridview should be used. You may extend this to give message if more than two rows selected.

Tassisto
  • 9,877
  • 28
  • 100
  • 157
Harshal Doshi Jain
  • 2,567
  • 1
  • 20
  • 16
0

You can use the SelectionChanged Event. CurrentRow.DataBoundItem will give the bound item.

SelectionMode property should be full row select.

var item = ([CastToBindedItem])dataGridLocations.CurrentRow.DataBoundItem; tbxEditLocation.Text = item.Name;

nadun
  • 21
  • 7