Gridview and getting row index

This is a truly stupid question. I have done this a million times with 1.1 and asp.net but cannot figure out how to do this using .net 2.0 and a WINDOWS application.

Basically I have a gridview control which contains some records.  The first column contains the primary key for my record.  When the user clicks on a row I want to extract the value held in the primarykey column so that I can get that record from my dataset and display it in a series of textboxes for editing.

I am using the CellContentClick event (not sure if this is correct) on my GridView and am extracting the current row "e.RowIndex" but how the hell do I get the contents of the first column called "PrimaryKey"

.



Answer this question

Gridview and getting row index

  • Wesley P

    string test;         
    int x = dataGridView2.CurrentCell.ColumnIndex; //column
    int y = dataGridView2.CurrentCell.RowIndex; //row
    test = dataGridView2[x, y].Value.ToString();

  • getter

    That will give me the rowindex number of the current cell not the contents of the cell at the 1st position in the current row.

  • spurandare

    Please tell me how to get the index of the gridview row without selecting the row and by just clicking the template column in that row.

    Please help!


  • Drew Noakes

    i've tested this. works fine.

    string test;         
    int x = dataGridView2.CurrentCell.ColumnIndex; //column
    int y = dataGridView2.CurrentCell.RowIndex; //row
    test = dataGridView2[x, y].Value.ToString();


  • Gridview and getting row index