【发布时间】:2017-05-09 12:47:56
【问题描述】:
我正在使用下面的代码来禁用 gridview 中列名为 test 的行。除第一行外,一切正常。颜色不会应用于第一行。我哪里出错了?我还想根据列名隐藏一列。
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Hidden4.Value == "Data Present")
{
foreach (GridViewRow item in GdvTestData.Rows)
{
int a = GetColumnIndexByName(item, "test");
int b = GetColumnIndexByName(item, "id");
if (e.Row.RowType == DataControlRowType.DataRow)
{
string test = e.Row.Cells[a].Text;
foreach (TableCell cell in e.Row.Cells)
{
if (test == "Y")
{
cell.BackColor = Color.Gray;
e.Row.Attributes.Add("onmouseover", "alert('This data is for testing');");
}
}
e.Row.Cells[b].Visible = false;
}
}
}
}
【问题讨论】:
-
当您已经可以访问
RowDataBound事件中的当前行 (e.Row) 时,为什么要循环GridViewRows(首先对每一行执行)? -
如何重写 .如果我不使用循环,则无法使用 GetColumnIndexByName。
-
循环错误。这意味着您将为每一行都这样做。
标签: c# asp.net gridview rowdatabound