Get gridview’s column value from code

1.If the column is BoundColumn:

GridViewRow gr = btn.NamingContainer as GridViewRow;

string to=gr.Cell(1).Text;

2. If the column is in a TemplateField and the data-binding expression is used directly in the ItemTemplate, the field value is automatically placed in DataBoundLiteral control.

    DataBoundLiteralControl yourColumnLiteral = (DataBoundLiteralControl)selectRow.Cells[1].Controls[0];
    string yourColumn = yourColumnLiteral.Text;

3. If the data is binded to an asp control, for example, in a label, you can use FindControl to get the text from that gridviewrow.

   Label lbl1 = (Label)gr.Cells[2].FindControl("myLabel1");


   Label lbl1 = (Label)gr.FindControl("myLabel1"); 
    string myString = lbl1.Text;

4. Of cource, you can access your datakey(s) from the DataKeyNames collection for your selected row.

int myIntKey = (Int32)GridView1.DataKeys[myGridViewRow.RowIndex]["myIntKey"];


int myIntKey = (Int32)GridView1.DataKeys[myGridViewRow.RowIndex][0];


int myIntKey = (Int32)GridView1.SelectedDataKey[0];



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s