如何在网格视图中绑定之前读取特定列中的所有行并进行更改 asp.net
how to read a all rows in a specific column and change before binding in grid view asp.net
我有一个 SQL Table,其中一列包含所有六进制值。
从网格中的 table 检索数据时 view.I 需要更改其在特定列中的所有值,然后绑定。
foreach (DataRow dr in ds.Tables[0].Rows)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
object o = dr["ColumnName"];
if (o != DBNull.Value) // Check for null
{
if (ds.Tables[0].Rows[i]["ColumnName"].ToString() != null)
{
ds.Tables[0].Rows[i]["ColumnName"] = value.ToString();
}
else
{ }
}
}
}
ds.Tables[0].AcceptChanges();
grid.DataSource = ds;
grid.DataBind();
我试图只遍历一个特定的列并更改其中的所有值。但它正在失败。请帮助我
首先,bind
你的 grid view
并添加 OnRowDataBound
喜欢
<asp:GridView ID="GridView1" runat="server" OnRowDataBound = "OnRowDataBound">
当 GridView
行绑定到数据时,RowDataBound
事件是 triggered
for each GridView Row
。
然后在你的OnRowDataBound
事件代码
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>()
extension method to find values from specified column name, replace the values with 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
类型参数指定的类型,因此以下 if 条件使用检查 null
或空字符串而不是 DBNull.Value
.
我有一个 SQL Table,其中一列包含所有六进制值。 从网格中的 table 检索数据时 view.I 需要更改其在特定列中的所有值,然后绑定。
foreach (DataRow dr in ds.Tables[0].Rows)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
object o = dr["ColumnName"];
if (o != DBNull.Value) // Check for null
{
if (ds.Tables[0].Rows[i]["ColumnName"].ToString() != null)
{
ds.Tables[0].Rows[i]["ColumnName"] = value.ToString();
}
else
{ }
}
}
}
ds.Tables[0].AcceptChanges();
grid.DataSource = ds;
grid.DataBind();
我试图只遍历一个特定的列并更改其中的所有值。但它正在失败。请帮助我
首先,bind
你的 grid view
并添加 OnRowDataBound
喜欢
<asp:GridView ID="GridView1" runat="server" OnRowDataBound = "OnRowDataBound">
当 GridView
行绑定到数据时,RowDataBound
事件是 triggered
for each GridView Row
。
然后在你的OnRowDataBound
事件代码
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>()
extension method to find values from specified column name, replace the values with 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
类型参数指定的类型,因此以下 if 条件使用检查 null
或空字符串而不是 DBNull.Value
.