GridView表的第一行缺少数据

时间:2016-03-08 20:49:48

标签: asp.net .net vb.net gridview

当我调用gvTags.DataSource = GetTags()和gvTags.DataBind()时,它会显示所有行和列都有适当的数据。

当命中OnRowDataBound函数时,我的第一个表行中的最后一列没有数据(空值)。

此表与我创建的其他表之间的唯一区别是,我根据页面中较高的用户选择显示/隐藏列。但是 - 显示/隐藏是在OnRowDataBound函数内完成的,其中数据已经丢失。我没有知道发生了什么,或者甚至开始寻找更远的地方。

UPDATE:看起来这个问题是由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

1 个答案:

答案 0 :(得分:2)

我的建议:如果根据网格外的CheckBox显示/隐藏列,可以在标记中执行此操作:

<asp:CheckBox ID="chkBox" runat="server" AutoPostBack="true" ... />

在代码隐藏中处理事件:

Private Sub chkBox_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkBox.CheckedChanged
    gvTags.Columns(removedByColumnIndex).Visible = chkBox.Checked
    gvTags.Columns(removedDateColumnIndex).Visible = chkBox.Checked
    gvTags.Columns(descriptionColumnIndex).Visible = (Not chkBox.Checked)
    gvTags.DataSource = GetTags()
    gvTags.DataBind()
End Sub