根据绑定数据表的单元格值设置元素可见性

Set an Elements Visibility Based on Cell Value of Bound Datatable

我正在尝试根据绑定 DataTable 中的关联列找出 GridView 中的单元格 可见 的方法。我认为我遇到的问题是这个 table 是动态的,也许当行被添加到 table 时,table 不是 re-populated/re-freshed asp页。

场景如下: 我有一个 table,例如,三列和一行。第 1 列是索引,第 2 列是名称,第 3 列是 true 或 false,用于设置 link 在 asp 页面中的可见性。

第 1 行已设置为 1, John Doe, false,因此在 asp 页面上您看到的只是

1 | John Doe

您可以点击下拉菜单,点击一个名字,然后将这个名字添加到数据中table。默认情况下,第 3 列插入一个 true 值。因此,在将行插入 table 后,该行将反映在 asp 页面上

1 | John Doe
2 | Jane Doe

但是因为第 3 列是正确的,所以我希望 'delete' 按钮可见,这样 asp 页面看起来像这样

1 | John Doe
2 | Jane Doe | Delete

其中 'Delete' 是 link 用于删除新插入的行。

我发现 this 认为这正是我需要的,但是 'Delete' link 仍然无法在 GridView 中显示。

我忽略了什么,以便我可以根据 DataTable 中的单元格值简单地显示 asp:LinkButton(或任何 link 等价物)?

编辑

添加 RowDataBound 事件处理函数。

protected void NamesGV_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        for (int i = 0; i < e.Row.Cells.Count; i++) {
            e.Row.Cells[i].BackColor = System.Drawing.Color.Beige; ;
        }
    }
}

像这样的东西...未经测试,因此您可能需要稍微调整一下:

 protected void NamesGV_RowDataBound(object sender, GridViewRowEventArgs e) 
 {
    if (e.Row.RowType == DataControlRowType.Header) 
    {
        for (int i = 0; i < e.Row.Cells.Count; i++) 
        {
            e.Row.Cells[i].BackColor = System.Drawing.Color.Beige;
        }
    }

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         //Get reference to the button you want to hide/show
         LinkButton delButton = CType(e.Row.Cells(2).FindControl("lbYourLinkButtonId"), LinkButton);

         //check your data for value
         bool visible = (bool)DoACheckForAValue(NamesGV.DataKeys(e.Row.RowIndex).Value);
         delButton.Visible = visible;
    }
}