首先,绑定你的 grid view 并添加 OnRowDataBound 喜欢
grid view
OnRowDataBound
<asp:GridView ID="GridView1" runat="server" OnRowDataBound = "OnRowDataBound">
RowDataBound 事件被触发 for each GridView Row 当。。。的时候 GridView 行绑定到数据。 然后在你的 OnRowDataBound 事件代码
RowDataBound
for each GridView Row
GridView
protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { column1 = e.Row.Cells[1].Text; //here you can give the column no that you want get like e.Row.Cells[1] e.Row.Cells[1].Text="test"; //you can set what you want like this } }
以来 ds.Tables[0] 包含 DataTable 对象,你可以使用 DataRow.Field<T>() 扩展方法从指定的列名中查找值,用值替换值 SetField() 然后重新绑定对网格数据源的更改:
ds.Tables[0]
DataTable
DataRow.Field<T>()
SetField()
foreach (DataRow dr in ds.Tables[0].Rows) { string oldValue = dr.Field<string>("ColumnName"); // check if the column has value if (!string.IsNullOrEmpty(oldValue)) { dr.SetField("ColumnName", value.ToString()); } else { // do something else } } ds.Tables[0].AcceptChanges(); // rebind the data source here
注意 DataRow.Field<T> 将值转换为指定的类型 T type参数,因此以下if-condition使用check对 null 或空字符串而不是 DBNull.Value 。
DataRow.Field<T>
T
null
DBNull.Value