如何在gridview中隐藏templatefield

时间:2013-03-17 16:08:05

标签: c# asp.net

我正在重复这个问题,因为我无法从任何地方找到答案。

我在.aspx页面中有一个GridView。我想基于aspx.cs BindData()方法隐藏列。

我尝试使用下面的代码,但无法隐藏。我正在使用带有C#的Asp.net。

以下是我的GridView列,我还添加了Button点击代码。

如果我select "T-L"位于else-if Ladder以下,我将收到此error

DataBinding:'System.Data.DataRowView'不包含名为'tutorial'的属性。
我在.aspx中标记了一个测试用例错误

暂时让我的程序工作我使用4 gridview来绑定4个查询,这是不可行的......我如何根据条件隐藏模板字段隐藏... 请帮助我..

<GridView>
    <Columns>
        <asp:BoundField DataField="id" HeaderText="Id" SortExpression="id" 
            Visible="false" />                     
        <asp:TemplateField HeaderText="RollNo" >
            <ItemTemplate>
                <%# Eval("st_rollno")%>
            </ItemTemplate>
            <EditItemTemplate>    
                <asp:TextBox ID="tbsturollno" runat="Server"
                    Text='<%# Eval("st_rollno") %>'></asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <%# Eval("st_name")%>
            </ItemTemplate>
            <EditItemTemplate>    
                <asp:TextBox ID="tbstuname" runat="Server"
                    Text='<%# Eval("st_name") %>'></asp:TextBox>
            </EditItemTemplate>               
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Theory">
            <ItemTemplate>
                <%# Eval("theory")%>
            </ItemTemplate>
            <EditItemTemplate> 
                <asp:TextBox ID="tbtheory" runat="Server"
                    Text='<%# Eval("theory") %>'></asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Total" >
            <ItemTemplate>
                <%# Eval("ttotal")%>
            </ItemTemplate>
            <EditItemTemplate>   
                <asp:TextBox ID="tbtheorytotal" runat="Server"
                    Text='<%# Eval("ttotal") %>'></asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Lab" >
            <ItemTemplate>
                <%# Eval("lab")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="tblab" runat="Server"
                    Text='<%# Eval("lab") %>'>
                </asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Total" >
            <ItemTemplate>
                <%# Eval("ltotal")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="tblabtotal" runat="Server"
                    Text='<%# Eval("ltotal") %>'></asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Tutorial" >
            <ItemTemplate>
   *Error is HERE             <%# Eval("tutorial")%>
            </ItemTemplate>
            <EditItemTemplate>   
                <asp:TextBox ID="tbtutorial" runat="Server"
                    Text='<%# Eval("tutorial") %>'></asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Total" >
            <ItemTemplate>
                <%# Eval("tutotal")%>
            </ItemTemplate>
            <EditItemTemplate>    
                <asp:TextBox ID="tbtutorialtotal" runat="Server"
                    Text='<%# Eval("tutotal") %>'></asp:TextBox>
            </EditItemTemplate>   
        </asp:TemplateField>        
    </Columns>
</GridView>

private void BindData()
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        if (stype.Equals("L"))
        {
            query = "SELECT [id], [st_rollno], [st_name], [lab], [ltotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
            GridView1.Columns[3].Visible = false;
            GridView1.Columns[4].Visible = false;
            GridView1.Columns[7].Visible = false;
            GridView1.Columns[8].Visible = false;
        }
        else if (stype.Equals("T"))
        {
            query = "SELECT [id], [st_rollno], [st_name], [theory], [ttotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
            GridView1.Columns[5].Visible = false;
            GridView1.Columns[6].Visible = false;
            GridView1.Columns[7].Visible = false;
            GridView1.Columns[8].Visible = false;
        }
        else if (stype.Equals("T-L"))
        {
            GridView1.Columns[7].Visible = false;
            GridView1.Columns[8].Visible = false;    
            query = "SELECT [id], [st_rollno], [st_name], [theory], [ttotal], [lab], [ltotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
        }
        else
        {    
            query = "SELECT [id], [st_rollno], [st_name],[theory], [ttotal], [lab], [ltotal], [tutorial], [tutotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";
        }

        com = new SqlCommand(query);
        com.Parameters.Add("@branch_name", dept);
        com.Parameters.Add("@scode", dpsubject.SelectedItem.Text.ToString());
        com.Parameters.Add("@sem_no", Int32.Parse(dpsemno.SelectedItem.Text.ToString()));
        com.Parameters.Add("@sess_no",Int32.Parse(dpsessional.SelectedItem.Text.ToString()));

        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            com.Connection = con;
            con.Open();
            sda.SelectCommand = com;
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();
        }    
    }
}

protected void bsubmit_Click(object sender, EventArgs e)
{
    this.BindData();
}

1 个答案:

答案 0 :(得分:9)

尝试让您的查询仍然为您要隐藏的每个列返回结果,即使您不使用这些值。在查询stype何时为“T-L”时,请从此处更改query

query = "SELECT [id], [st_rollno], [st_name], [theory], [ttotal], [lab], [ltotal] FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";

对此:

query = "SELECT [id], [st_rollno], [st_name], [theory], [ttotal], [lab], [ltotal], '' as tutorial, '' as tutotal FROM [Attendence_Subject_Wise] WHERE (([branch_name] = @branch_name) AND ([scode] = @scode) AND ([sem_no] = @sem_no) AND ([sess_no] = @sess_no)) ORDER BY [st_rollno]";

修改

要隐藏列,请将以下方法添加到后面的代码中:

protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{
    if (stype.Equals("T-L"))
    {
        MyGridView.Columns[7].Visible = false;
        MyGridView.Columns[8].Visible = false;
    }
}

并在GridView上订阅DataBound事件。请注意,我在GridView中添加了一个ID,因此我可以从后面的代码中引用它。

<GridView ID="MyGridView" OnDataBound="GridView_DataBound">