【发布时间】:2016-03-08 20:49:48
【问题描述】:
当我调用 gvTags.DataSource = GetTags() 和 gvTags.DataBind() 时,它显示所有行和列都有适当的数据。
当 OnRowDataBound 函数被点击时,我的第一个表行中的最后一列没有数据(空值)。
此表与我创建的其他表之间的唯一区别是,我根据页面上方的用户选择显示/隐藏列。但是 - 显示/隐藏是在 OnRowDataBound 函数中完成的,其中数据已经丢失。我不知道发生了什么,甚至不知道从哪里开始进一步寻找。
更新: 看起来问题是由 RowDataBound 函数的最后三行引起的。当我删除这三行时,数据会按原样显示。所以 - 我需要一种方法来显示/隐藏这三列,基于用户是否在页面上的其他地方选择了一个复选框(包括已删除的条目)。如果选中复选框 ,则 RemovedBy 和 RemovedDate 列可见。如果它没有被选中,那些隐藏但描述列是可见的。
<asp:GridView ID="gvTags" CssClass="table table-striped" runat="server" AutoGenerateColumns="false"
AllowSorting="false" GridLines="None" OnRowDataBound="gvTags_RowDataBound" DataKeyNames="TagID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnRemove" runat="server" OnClick="btnRemove_Click">
Remove
</asp:LinkButton>
<asp:LinkButton ID="btnReapply" runat="server" OnClick="btnReapply_Click">
Re-Apply
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TagID" HeaderText="ID" />
<asp:BoundField DataField="InstalledBy" HeaderText="Installed By" />
<asp:BoundField DataField="InstalledDate" HeaderText="Date Installed" />
<asp:BoundField DataField="RemovedBy" HeaderText="Removed By" />
<asp:BoundField DataField="RemovedDate" HeaderText="Date Removed" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="IsRemoved" Visible="false" />
</Columns>
</asp:GridView>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If (Page.IsPostBack) Then
Exit Sub
End If
gvTags.DataSource = GetTags()
gvTags.DataBind()
'NOTE - Here, DataSource has complete data (all rows & columns that should have data, do have data)
If (gvTags.Rows.Count > 0) Then
gvTags.HeaderRow.TableSection = TableRowSection.TableHeader
End If
End Sub
Protected Sub gvTags_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (Not e.Row.RowType = DataControlRowType.DataRow) Then
Exit Sub
End If
e.Row.FindControl("btnRemove").Visible = Not Boolean.Parse(DataBinder.Eval(e.Row.DataItem, "IsRemoved").ToString)
e.Row.FindControl("btnReapply").Visible = Boolean.Parse(DataBinder.Eval(e.Row.DataItem, "IsRemoved").ToString)
'NOTE - Here, gvTags.Columns(columnIndex) is null for each of the below three columns (RemovedBy, RemovedDate, and Description)
gvTags.Columns(removedByColumnIndex).Visible = checkbox.Checked
gvTags.Columns(removedDateColumnIndex).Visible = checkbox.Checked
gvTags.Columns(descriptionColumnIndex).Visible = (Not checkbox.Checked)
End Sub
【问题讨论】:
-
我不明白您的 RowDataBound 处理程序的最后 3 行。在处理每条记录时需要显示/隐藏每一列?如果是这样,最终结果将仅取决于最后一个数据行。我认为,您可以在绑定数据后执行此操作,因为复选框不在网格中(如果我没记错的话)。至于您的主要问题,如果这 3 行不存在,您是否看到所有数据?
-
InstalledBy 和 InstalledDate 行将无论如何显示。用户可以选择显示/隐藏已删除的记录(页面上方有一个复选框 - 不在网格视图中,正确)。如果未显示已删除的记录,则隐藏已删除的列。隐藏已删除的列时会显示说明,因为我现在有空间包含它,并且客户希望尽可能包含它。
-
我发布了一个答案,假设列的显示/隐藏仅取决于 CheckBox(如您的代码所示)。您的评论似乎暗示了一些不同的东西......
标签: asp.net .net vb.net gridview