有条件地显示基于Checkboxlist的网格视图列

时间:2018-01-10 15:39:00

标签: c# asp.net gridview webforms

我有一个要求,我有两个网格视图。一个用于标题,一个用于实际数据。我希望标题网格从索引[0]开始,无论任何情况,但我希望数据网格基于索引[1]开始显示/隐藏(意味着无论复选框如何都将显示网格的前两行)选择。

我尝试了这种语法,但这是基于索引[1]的两个网格 - 我如何有条件地显示每个单独的网格?

    protected void ShowClickedOnly_Click(object sender, EventArgs e)
{
    gridviewData.Visible = true;
    gridViewHeader.Visible = true;

    var columnname = string.Empty;
    foreach (System.Web.UI.WebControls.ListItem item in checkboxlistPeople.Items)
    {
        columnname = item.Text;
        var index = GetColumnIndex(gridViewHeader, columnname);
        string number = index.ToString();
        if (index > 1)
        {
            gridViewHeader.Columns[index].Visible = item.Selected;
            gridviewData.Columns[index].Visible = item.Selected;
        }
    }
}
protected void checkboxlistPeople_SelectedIndexChanged(object sender, EventArgs e)
{
    var items = checkboxlistPeople.Items;
}

private int GetColumnIndex(GridView grid, string ColName)
{
    foreach (DataControlField col in grid.Columns)
    {
        if (col.HeaderText.ToLower().Trim() == ColName.ToLower().Trim())
        {
            return grid.Columns.IndexOf(col);
        }
    }
    return -1;
}

修改
为了解决这个问题,我试图变得狡猾并使用下面的内容 - 但是现在这会在网格的末尾返回一个“ghost”空列,因此返回4行而不是3行。

if (index > 0)
{
    gridViewHeader.Columns[index].Visible = item.Selected;
    gridviewData.Columns[index].Visible = item.Selected;
    if (index > 1)
    {
        gridViewHeader.Columns[index].Visible = item.Selected;
        gridviewData.Columns[index].Visible = item.Selected;
    }
}

修改
这是页面的HTML标记

    <div runat="server" id="checkboxlistPeople">    
    <asp:CheckBoxList ID="cbxSelected" runat="server" RepeatLayout="table" RepeatColumns="1" RepeatDirection="vertical" OnSelectedIndexChanged="informationSelected_SelectedIndexChanged"></asp:CheckBoxList>    
</div>
<div id="btnForData"><asp:Button ID="btnDisplayData" runat="server" onclick="btnDisplay_Click" Text="Display Information" /> </div>   

<div id="gridviewData" >
    <asp:GridView ID="gridviewHeader" runat="server" CssClass="Grid" AutoGenerateColumns="false"> 
        <Columns>                    
            <asp:BoundField DataField="Header" HeaderText="Header"  HeaderStyle-Width="50%" HeaderStyle-HorizontalAlign="Left"></asp:BoundField>
            <asp:BoundField DataField="DataElement1" HeaderText="DataElement1" HeaderStyle-Width="8%"></asp:BoundField>
            <asp:BoundField DataField="DataElement2" HeaderText="DataElement2" HeaderStyle-Width="8%"></asp:BoundField>
            <asp:BoundField DataField="DataElement3" HeaderText="DataElement4" HeaderStyle-Width="8%"></asp:BoundField>
        </Columns>
    </asp:GridView>
</div>
<asp:GridView ID="gridviewData" runat="server" CssClass="Grid" AutoGenerateColumns="false" ShowHeader="False"> 
    <Columns>
        <asp:templatefield HeaderText="Display"><itemtemplate><asp:checkbox ID="cbSelect" Checked="true" CssClass="gridCB" runat="server"></asp:checkbox></itemtemplate></asp:templatefield>
        <asp:BoundField DataField="Header" HeaderText="Header"  HeaderStyle-Width="50%" HeaderStyle-HorizontalAlign="Left"></asp:BoundField>
        <asp:BoundField DataField="DataElement1" HeaderText="DataElement1"  HeaderStyle-Width="50%" HeaderStyle-HorizontalAlign="Left"> </asp:BoundField>
        <asp:BoundField DataField="DataElement2" HeaderText="DataElement2" HeaderStyle-Width="8%"></asp:BoundField>
        <asp:BoundField DataField="DataElement3" HeaderText="DataElement3" HeaderStyle-Width="8%"></asp:BoundField>
    </Columns>
</asp:GridView>

0 个答案:

没有答案