2017-02-12 102 views
0

我有一個GridView,其中有10列。10我有5列命名爲Ata,Hplan,Hosp,其他(這4個是checkboxfield),其他顏色(Boundfield),再次Alta,Hplan等..現在我想要在用戶下拉選擇要麼5列添加或所有10 ..我需要禁用gridview視圖狀態?我怎麼能實現這一點和第二個問題是我怎麼能動態連續數據綁定event.Basic東西時綁定這些列的是,我希望gridview的列名來複制,但是在數據表中我不能給重複列names.please幫助..從asp.net動態數據表中動態綁定gridview列

protected void Btnshow_Click(object sender, EventArgs e) 
     { 
      if (DropDownList1.SelectedIndex==1) 
      { 
       dtgridbind.Columns.AddRange(new DataColumn[10] { new DataColumn("1_ALta", typeof(bool)),new DataColumn("1_Hplan", typeof(bool)), 
          new DataColumn("1_Hosp",typeof(bool)),new DataColumn("1_other",typeof(bool)),new DataColumn("1_othercolor",typeof(string)), 
          new DataColumn("2_ALta", typeof(bool)),new DataColumn("2_Hplan", typeof(bool)), 
          new DataColumn("2_Hosp",typeof(bool)),new DataColumn("2_other",typeof(bool)),new DataColumn("2_othercolor",typeof(string))}); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red"); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red"); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red"); 
       GridView1.DataSource = dtgridbind; 
       GridView1.DataBind(); 
       // ViewState["modechange"] = 1; 
      } 
      else 
      { 
       dtgridbind.Columns.AddRange(new DataColumn[5] { new DataColumn("1_ALta", typeof(bool)),new DataColumn("1_Hplan", typeof(bool)), 
          new DataColumn("1_Hosp",typeof(bool)),new DataColumn("1_other",typeof(bool)),new DataColumn("1_othercolor",typeof(string))}); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A"); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A"); 
       dtgridbind.Rows.Add(true, false, false, false, "Red"); 
       // ViewState["grid"] = dtgridbind; 
       GridView1.DataSource = dtgridbind; 
       GridView1.DataBind(); 
      } 
     } 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.Header) 
      { 

       foreach (DataColumn datacol in dtgridbind.Columns) 
       { 
        if (datacol.ColumnName.Contains("_othercolor")) 
        { 
         BoundField bfield = new BoundField(); 
         bfield.HeaderText = datacol.ColumnName.Substring(2, datacol.ColumnName.Length - 2); 
         bfield.DataField = datacol.ColumnName; 
         GridView1.Columns.Add(bfield); 
        } 

        else 
        { 
         CheckBoxField ck = new CheckBoxField(); 
         ck.HeaderText = datacol.ColumnName.Substring(2, datacol.ColumnName.Length - 2); 
         ck.DataField = datacol.ColumnName; 
         GridView1.Columns.Add(ck); 





        } 
       } 
      } 

回答

0

這段代碼做什麼你要?在這個代碼中,有一個包含兩個值的下拉列表。如果選擇「10列」,則回發並將10列添加到數據表。如果選擇「5列」,則回發並將5列添加到數據表。

在OnRowDataBound中,它循環遍歷標題單元格,並根據「_」字符分割文本。然後它接受第二部分。如果您按照列的命名約定,這將僅適用。

您不能使用具有相同列名稱的數據列。爲了解決這個問題,你可以使用OnRowDataBound,並按上面所述修改單元格文本。結果

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true"> 
      <asp:ListItem Text="" Value =""></asp:ListItem> 
      <asp:ListItem Text="10 Columns" Value ="1"></asp:ListItem> 
      <asp:ListItem Text="5 Columns" Value ="2"></asp:ListItem> 
     </asp:DropDownList> 
     <asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound"></asp:GridView> 
    </form> 
</body> 
</html> 


public partial class WebForm3 : System.Web.UI.Page 
    { 
     private DataTable dtgridbind = new DataTable(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.Header) 
      { 
       foreach (TableCell headerCell in e.Row.Cells) 
       { 
        headerCell.Text = headerCell.Text.Split('_')[1]; 
       } 
      } 
     } 

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (DropDownList1.SelectedIndex == 1) 
      { 
       dtgridbind.Columns.AddRange(new DataColumn[10] { new DataColumn("1_ALta", typeof(bool)),new DataColumn("1_Hplan", typeof(bool)), 
          new DataColumn("1_Hosp",typeof(bool)),new DataColumn("1_other",typeof(bool)),new DataColumn("1_othercolor",typeof(string)), 
          new DataColumn("2_ALta", typeof(bool)),new DataColumn("2_Hplan", typeof(bool)), 
          new DataColumn("2_Hosp",typeof(bool)),new DataColumn("2_other",typeof(bool)),new DataColumn("2_othercolor",typeof(string))}); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red"); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red"); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A", false, true, false, false, "Red"); 
       GridView1.DataSource = dtgridbind; 
       GridView1.DataBind(); 
      } 
      else 
      { 
       dtgridbind.Columns.AddRange(new DataColumn[5] { new DataColumn("1_ALta", typeof(bool)),new DataColumn("1_Hplan", typeof(bool)), 
          new DataColumn("1_Hosp",typeof(bool)),new DataColumn("1_other",typeof(bool)),new DataColumn("1_othercolor",typeof(string))}); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A"); 
       dtgridbind.Rows.Add(true, false, false, false, "N/A"); 
       dtgridbind.Rows.Add(true, false, false, false, "Red"); 
       GridView1.DataSource = dtgridbind; 
       GridView1.DataBind(); 
      } 
     } 
    } 

截圖: enter image description here

+0

heyyy ..thanks buddy..but蔭沒有得到內部headercell.text.I任何文本還沒有gridview.I靜態定義列真不知道是什麼原因這是發生..有人知道? –

+0

我在帖子的底部添加了結果的屏幕截圖。這與你得到的結果相符嗎?數據表綁定到gridview,因此列名將是headercell文本。當我調試代碼時,headercell.text填充了數據表列名稱。 –