I am repeating this question because i am not able to find answer from anywhere.
I Have a GridView
in .aspx page. I want to hide columns based on aspx.cs BindData()
method.
I have tried using below code but not able to hide. I am using Asp.net with C#.
Below is my GridView
with columns and I have also included the Button
click code.
If I select "T-L"
which is in below else-if Ladder
i am getting this error
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'tutorial'.
I have marked a test case error in .aspx
For temporary to make my program work i am using 4 gridview to bind 4 query,which is not feasible...How can i hide templatefield invisible based on conditions... plz help me..
<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();
}
Try having your query still return a result for each column you want to hide even though you won't use those values. In the query for when stype
is "T-L", change query
from this:
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]";
To this:
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]";
EDIT:
To hide the column, add the following method to your code behind:
protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{
if (stype.Equals("T-L"))
{
MyGridView.Columns[7].Visible = false;
MyGridView.Columns[8].Visible = false;
}
}
And subscribe to the DataBound event on the GridView. Notice that I added an ID to the GridView so I can reference it from the code behind.
<GridView ID="MyGridView" OnDataBound="GridView_DataBound">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With