How to get cell value in GridView (WITHOUT using cell index)

前端 未结 3 1716
春和景丽
春和景丽 2021-01-14 22:53

how to get cell value from gridview without using cell index? Let say the first column name in my table is \"RowNumber\".

instead of using

string nam         


        
3条回答
  •  我在风中等你
    2021-01-14 23:26

    You could cast the GridViewRow's DataItem property into a DataRowView, and then reference the column names:

    DataRowView rowView = (DataRowView)GridView1.Rows[0].DataItem;
    string name = rowView["RowNumber"].ToString();
    

    You can't do this from the Cells collection, because they are just TableCell objects, and they don't know anything about the underlying data.

    The DataItem property represents the values in that row from the underlying datasource, so that's what you want to deal with.

提交回复
热议问题