在ASP.NET中显示/隐藏GridView列

时间:2014-09-02 06:25:08

标签: c# asp.net gridview

我正在使用C#在Visual Studio 2012中创建一个Web项目,该项目从数据库中获取数据并在网格视图中显示。数据库中有许多列,它们在网格视图中显示。我想为用户提供一个选项,使用复选框消除不需要的列,检查后,点击按钮就必须更新。

直到现在我已经尝试过如下所示的代码,但没有运气。

protected void btn_modify_Click(object sender, EventArgs e)
{
    if (CheckBox2.Checked)
    {

        this.GridView1.Columns[2].Visible = false;        

    }
    else
    {        
        this.GridView1.Columns[2].Visible = true;
    }
}

.aspx代码如下。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Reporting Mopdule.aspx.cs" Inherits="WebApplication1.Reporting_Mopdule" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajax" %>

<!DOCTYPE html>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .newStyle1 {
            font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
            font-size: x-large;
        }
        .newStyle2 {
            font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
            color: #0000FF;
        }
        .newStyle3 {
            font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
            color: #0000FF;
        }
        .newStyle4 {
            color: #0000FF;
            font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
        }
        .newStyle5 {
            font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
            color: #0000FF;
        }
        .auto-style3 {
            width: 122px;
        }
        .newStyle6 {
            font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
            font-size: x-large;
        }
        .newStyle7 {
            position: absolute;
        }
        .auto-style5 {
            width: 117px;
        }
        .auto-style7 {
            width: 59px;
        }
        .auto-style8 {
            width: 65px;
        }
        .newStyle8 {
            position: absolute;
        }
        .newStyle9 {
            position: absolute;
        }
        .auto-style9 {
            width: 305px;
        }
        .newStyle10 {
            position: absolute;
        }
        .newStyle11 {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <form id="form1" runat="server">
    <div>
    <ajax:ToolkitScriptManager ID="toolkit1" runat="server"></ajax:ToolkitScriptManager>
    </div>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <table style="width:100%;">
            <caption style="width: 478px">
                <asp:Label ID="Label6" runat="server" CssClass="newStyle6" Font-Bold="True" Text="Report On Energy Development"></asp:Label>
            </caption>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
&nbsp;
        <table style="width: 69%; margin-top: 0px; height: 303px;">
            <caption>
                <br />
            </caption>
            <tr>
                <td class="auto-style5">
                    <asp:Label ID="Label1" runat="server" CssClass="newStyle2" Text="State"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label2" runat="server" CssClass="newStyle3" Text="Cluster"></asp:Label>
                </td>
                <td class="auto-style9">
                    <asp:Label ID="Label3" runat="server" CssClass="newStyle4" Text="DateFrom : "></asp:Label>
                </td>
                <td class="auto-style3">
                    <asp:Label ID="Label4" runat="server" CssClass="newStyle5" Text="DateTo :"></asp:Label>
                </td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">
                    <asp:Button ID="Button3" runat="server" Height="34px" OnClick="Button3_Click" Text="Export To Excel" Width="138px" />
                </td>
                <td class="auto-style8" rowspan="11">
                    <asp:Panel ID="Panel1" runat="server" Width="224px">
                        <table style="width: 99%;">
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox17" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox17_CheckedChanged" Text="All" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox2" runat="server" Text="SerialNo" OnCheckedChanged="CheckBox2_CheckedChanged" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox11" runat="server" Text="Load_Enegy_Cumulative" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox3" runat="server" Text="SiteName" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox12" runat="server" Text="Mains_Energy_Daily" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox4" runat="server" Text="SiteId" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox13" runat="server" Text="Mains_Energy_Cumulative" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox5" runat="server" Text="State" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox14" runat="server" Text="Solar_Energy_Daily" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox6" runat="server" OnCheckedChanged="CheckBox6_CheckedChanged" Text="Cluster" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox15" runat="server" Text="Solar_Energy_Cumulative" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox7" runat="server" Text="Date" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox16" runat="server" Text="Generate_Date" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox8" runat="server" Text="Dg_Energy_Daily" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox9" runat="server" Text="Dg_Energy_Cumulative" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBox ID="CheckBox10" runat="server" Text="Load_Energy_Daily" />
                                </td>
                            </tr>
                        </table>
                    </asp:Panel>
                </td>
            </tr>
            <tr>
                <td class="auto-style5">
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                    </asp:DropDownList>
                </td>
                <td class="auto-style9">
                    <asp:TextBox ID="TextBox1" runat="server"  style="margin-top: 0px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                    <ajax:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox1">
                    </ajax:CalendarExtender>
                </td>
                <td class="auto-style3">
                    <asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged" ></asp:TextBox>
                    <ajax:CalendarExtender ID="TextBox2_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox2">
                    </ajax:CalendarExtender>
                </td>
                <td class="auto-style7">
                    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
                </td>
                <td class="auto-style8">
                    <asp:Button ID="Button4" runat="server" Height="30px" OnClick="Button4_Click" Text="Export To PDF" Width="139px" />
                </td>
            </tr>
            <tr>
                <td class="auto-style5">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td class="auto-style9">
                    &nbsp;</td>
                <td class="auto-style3">
                    <asp:TextBox ID="TextBox3" runat="server" AutoPostBack="True"></asp:TextBox>
                </td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">
                    <asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="Hide" Width="44px" />
                    <asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="Show" />
                </td>
            </tr>
            <tr>
                <td class="auto-style5">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td class="auto-style9">
                    &nbsp;</td>
                <td class="auto-style3">
                    &nbsp;</td>
                <td class="auto-style7">
                    &nbsp;</td>
                <td class="auto-style8">
                    <asp:Button ID="Button7" runat="server" Height="31px" OnClick="Button7_Click" Text="Modify Table" Width="139px"
                        />
                </td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
                <td class="auto-style9">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
                <td class="auto-style9">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
                <td class="auto-style9">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
                <td class="auto-style9">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td class="auto-style9">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style7">&nbsp;</td>
                <td class="auto-style8">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td class="auto-style9">
                    &nbsp;</td>
                <td class="auto-style3">
                    &nbsp;</td>
                <td class="auto-style7">
                    &nbsp;</td>
                <td class="auto-style8">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td class="auto-style9">
                    &nbsp;</td>
                <td class="auto-style3">
                    &nbsp;</td>
                <td class="auto-style7">
                    &nbsp;</td>
                <td class="auto-style8">
                    &nbsp;</td>
            </tr>
        </table>
&nbsp;&nbsp;<table style="width:100%;">
            <tr>
                <td>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" CellPadding="3" Height="185px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="297px" AutoGenerateColumns="false" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2">
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                    <PagerTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </PagerTemplate>
                    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#FFF1D4" />
                    <SortedAscendingHeaderStyle BackColor="#B95C30" />
                    <SortedDescendingCellStyle BackColor="#F1E5CE" />
                    <SortedDescendingHeaderStyle BackColor="#93451F" />
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
                </td>
            </tr>
        </table>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
    </form>
</body>
</html>

在下图中,您可以看到许多复选框。我希望用户通过单击复选框选择所需的所有列,并在单击“修改表”按钮后,仅在网格视图中显示这些列。

enter image description here

4 个答案:

答案 0 :(得分:1)

您可以使用以下代码

protected void btn_modify_Click(object sender, EventArgs e)
{
      foreach (ListItem val in CheckBoxList.Items)
      {
          if (val.Checked)
           {
               this.GridView1.Columns[0].Visible = false;
                               .
                               .
                               .
           }
          else
           {
               this.GridView1.Columns[0].Visible = true;
           }
      }
}

答案 1 :(得分:0)

不要删除列:GridView1.Columns.RemoveAt(2);

protected void btn_modify_Click(object sender, EventArgs e)
{
    GridView1.Columns[1].Visible = false;
    GridView1.Columns[0].Visible = false;
    if (CheckBox2.Checked)
    {

        TextBox3.Enabled = false;
        //GridView1.Visible = false; --> Hides the whole Gridview
        this.GridView1.Columns[2].Visible = false;
        //GridView1.Columns.RemoveAt(2);

    }
    else
    {
        TextBox3.Enabled = true;
        GridView1.Visible = true;
        this.GridView1.Columns[2].Visible = true;

    }
}

如果您的列是autogeneratet(默认= true),那么您可以通过创建CELL来更改可见性..如果所有单元格都不可见,则列将自动隐藏

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //ShowColumn Check
        if (CheckBox2.Checked)
        {
            e.Row.Cells[2].Visible = false;
        }
        else
        {
            e.Row.Cells[2].Visible = true;
        }   
    }
}

仅显示选定的行:

1。)添加一个新的templateColumn(在第一个位置):

<Columns>
   <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox runat="server"/>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

2。)添加一个新按钮

<asp:Button runat="server" ID="FilterSelected" OnClick="FilterSelected_Click"/>

3.)添加一个新的ClickEvent 代码:

protected void FilterSelected_Click(object sender, EventArgs e)
{
    foreach (GridViewRow rowItem in gridview1.Rows )
    {
        var myCheckbox = rowItem.Cells[0].Controls[1] as CheckBox; // ONLY if your Checkbox in the FIRST column!!!!

            if (myCheckbox != null && !myCheckbox.Checked)
            {
                rowItem.Visible = false;
            }   
        }
    }
}

答案 2 :(得分:0)

试试这个, 使用列名称可以实现此目的。

<asp:GridView ID="GridView1" runat="server" CellPadding="3" Height="185px" Width="297px"
     BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"  CellSpacing="2">
                        <Columns>
                            <asp:BoundField HeaderText="ColumnName" DataField="ColumnName" />
                            <%--Add Boundfield as per your columns--%>
                            <asp:BoundField HeaderText="ColumnName" DataField="ColumnName" />
                        </Columns>
</asp:GridView>

答案 3 :(得分:0)

在gridview的行创建事件中尝试此代码:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if(checkbox2.checked)
e.Row.Cells[2].Visible = false; 
else
e.Row.Cells[2].Visible = true;
}

在按钮上单击jus调用绑定网格的方法