2013-02-15 148 views
0

我在.aspx頁面中有一個GridView,我想隱藏基於aspx.cs BindData()方法的列。我曾嘗試使用下面的代碼,但無法隱藏。我在C#中使用Asp.net。使用ItemTemplate隱藏GridView列

以下是我的GridView列,我還包括Button點擊代碼。

<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> 
       <%# 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(); 
} 

回答

0
<asp:GridView runat="server" ID="GridView1" 
    AutoGenerateColumns="False" OnPreRender="GridView_PreRender"> 
    <Columns> 
     <asp:BoundField DataField="LastName" HeaderText="LastName" /> 
     <asp:BoundField DataField="FirstName" HeaderText="FirstName" /> 
    </Columns> 
</asp:GridView> 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     var users = new List<User> 
      { 
       new User {FirstName = "John", LastName = "Doe"}, 
       new User {FirstName = "Marry", LastName = "Newton"}, 
       new User {FirstName = "Joe", LastName = "Black"} 
      }; 

     GridView1.DataSource = users; 
     GridView1.DataBind(); 
    } 
} 

protected void GridView_PreRender(object sender, EventArgs e) 
{ 
    foreach (DataControlField column in GridView1.Columns) 
     if (column.HeaderText == "FirstName") 
      column.Visible = false; 
} 
+0

使列'visible = false'應該可以正常工作......執行PreRender對於一個簡單的問題是過度殺傷性的。 – Kelsey 2013-02-15 19:41:03

0

隨着GrideView.DataBind()你調用一個回傳,將撤消任何改變你到前端發。嘗試在DataBind之後對列進行處理。