How to get the cell value by column name not by index in GridView in asp.net

后端 未结 10 1597
轻奢々
轻奢々 2020-12-01 06:21

I am having a gridview in asp.net and now I want the cell value by the column name but not by the cell index.

How would be it possible

10条回答
  •  暖寄归人
    2020-12-01 06:54

    Although its a long time but this relatively small piece of code seems easy to read and get:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       int index;
       string cellContent;
    
        foreach (TableCell tc in ((GridView)sender).HeaderRow.Cells)
        {
           if( tc.Text.Equals("yourColumnName") )
           {
             index = ((GridView)sender).HeaderRow.Cells.GetCellIndex(tc);
             cellContent = ((GridView)sender).SelectedRow.Cells[index].Text;
             break;
           }
        }
    }
    

提交回复
热议问题