Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当DropDownList筛选器的值不是ALL时,如何删除GridView中的整个第一列?_C#_Asp.net - Fatal编程技术网

C# 当DropDownList筛选器的值不是ALL时,如何删除GridView中的整个第一列?

C# 当DropDownList筛选器的值不是ALL时,如何删除GridView中的整个第一列?,c#,asp.net,C#,Asp.net,我有以下复杂的asp.net代码结构: 作为过滤器的下拉列表 中继器 在中继器内部:我有HiddenField和GridView 我想当Filter的值设置为All时,应该删除第一列。问题是我使用的StoredProcess负责生成三个GridView,这样我就把GridView放在了中继器中 ASP.NET代码: <asp:DropDownList ID="ddlDivision" runat="server" AppendDataBoundItems="True" A

我有以下复杂的asp.net代码结构:

  • 作为过滤器的下拉列表
  • 中继器
  • 在中继器内部:我有HiddenField和GridView
  • 我想当Filter的值设置为All时,应该删除第一列。问题是我使用的StoredProcess负责生成三个GridView,这样我就把GridView放在了中继器中

    ASP.NET代码:

    <asp:DropDownList ID="ddlDivision" runat="server" AppendDataBoundItems="True" 
            AutoPostBack="True" DataSourceID="sqlDataSourceDivision" DataTextField="DivisionName" 
            DataValueField="DivisionName"  
            Width="275px" EnableViewState="False">
            <asp:ListItem Value="%">All</asp:ListItem>
        </asp:DropDownList>
    
    
         <br />  <br />  
            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>
    
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
    
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                        SelectCommandType="StoredProcedure" SelectCommand="kbiReport"
                                        FilterExpression="[DivisionName] like '{0}%'">
    
                        <FilterParameters>
                            <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 
                                                     PropertyName="SelectedValue" Type="String" />
                        </FilterParameters>
    
                        <SelectParameters>
                            <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values 
                                of GroupID--%>
                            <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <div style="width:700px; overflow:auto; overflow-y:hidden;">
    
                    <asp:GridView ID="GridView1" runat="server" 
                                    AllowSorting="True" 
                                    CellPadding="3" 
                                    DataSourceID="SqlDataSource1" 
                                    ClientIDMode="Static" class="fixedTables" Width="600" AutoGenerateColumns="true"
                                    AlternatingRowStyle-CssClass="alt"
                                    RowStyle-HorizontalAlign="Center" 
                                    OnRowDataBound="GridView1_RowDataBound" OnPreRender="GridView1_PreRender" OnRowCreated="GridView1_RowCreated"
                                    OnDataBound="GridView1_DataBound">
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <HeaderStyle Font-Bold = "true" ForeColor="Black"/> 
                        <Columns>
                        </Columns>
                        <EditRowStyle BackColor="#999999" />
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </asp:GridView>
                    </div>
                    <br />
                </ItemTemplate>
            </asp:Repeater>
    
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                               ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                               SelectCommand="SELECT DISTINCT GroupID FROM courses">
            </asp:SqlDataSource>
    
            <%--Filtering by Division--%>
            <asp:SqlDataSource ID="sqlDataSourceDivision" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT [DivisionName] FROM [Divisions]"></asp:SqlDataSource>
    
    我失败了有人能帮我解决这个问题吗?

    我想要当Filter的值设置为All时,第一列 应该删除

    如果是这种情况,您肯定希望:

    if (ddlDivision.SelectedValue == "ALL") { // hide column 0 } 
    
    与此相反:

    if (ddlDivision.SelectedValue != "ALL") { // hide column 0 } 
    
    使用
    gv.Columns[0].Visible=falsegv
    对象有一个正确的引用,并且网格视图的
    AutoGenerateColumns
    属性设置为
    false
    ,那么code>应该可以正常工作

    您必须确保这一点,因为
    AutoGenerateColumns=“true”
    是GridView的默认值,在这种情况下
    GridView.Columns.Count
    将始终为0。如果尝试引用
    .Columns[x]
    它将抛出“索引超出范围”异常

    更多信息请点击此处:

    更新:

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        ...
        if (e.Row.RowType != DataControlRowType.Pager) 
        {
            if (ddlDivision.SelectedItem.Text != "ALL")
            {
               // only check for pager row, all other rows including header/footer should be hidden
               e.Row.Cells[0].Visible = false; 
            }
        }
        ...    
    } 
    
    在使用
    AutoGenerateColumns=“true”
    时,可以使用GridView的
    RowDataBound
    事件隐藏列:

    创建事件处理程序:

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        ...
        if (e.Row.RowType != DataControlRowType.Pager) 
        {
            if (ddlDivision.SelectedItem.Text != "ALL")
            {
               // only check for pager row, all other rows including header/footer should be hidden
               e.Row.Cells[0].Visible = false; 
            }
        }
        ...    
    } 
    
    如果您的GridView是静态的(在.aspx页面上声明),请将其添加到声明中:
    onrowdabund=“gv_rowdabund”

    如果您的GridView是通过编程方式创建的,请添加RowDataBound事件处理程序:
    gv.RowDataBound+=新的GridViewRowEventHandler(gv_RowDataBound)

    然后添加行数据绑定事件:

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        ...
        if (e.Row.RowType != DataControlRowType.Pager) 
        {
            if (ddlDivision.SelectedItem.Text != "ALL")
            {
               // only check for pager row, all other rows including header/footer should be hidden
               e.Row.Cells[0].Visible = false; 
            }
        }
        ...    
    } 
    

    你是对的。我正在使用(AutoGenerateColumns=“true”),因为我正在将GridView绑定到StoredProcess,如果我将其设置为false,GridView中将不会显示任何内容。那么如何解决这个问题呢?好的,列现在被删除了,但是我如何在DroopDownList过滤器的值设置为ALL时删除它呢。请帮我解决这个问题,因为这对我来说是最困难的部分。顺便说一句,我非常感谢你的帮助和解释。请继续。没问题,请检查我的编辑,您可以将行事件包装在if中。非常感谢,但您所做的是在筛选器的值为“ALL”时隐藏列。我想要的是仅当过滤器的值为“ALL”时才显示第一列。怎么做?我把它设为(ddlDivision.SelectedValue!=“ALL”),但它不起作用,我不知道为什么。