2017-05-04 91 views
2

我有這個gridview,我需要填充超過25個數據庫表。這些表是查找表,並且這個gridview用於查找表維護目的。從Gridview |獲取動態創建的複選框值Asp.net

而不是在gridview中放置特定的列我直接綁定dataTable到gridview。

這下面的方法將(活動列的最後一列)活動列的值分配給複選框「y」/「n」。

<asp:GridView ID="gvTables" runat="server" CssClass="Grid" Width="100%" AutoGenerateColumns="true" Font-Names="Arial" 
            BorderWidth="1" PagerSettings-Mode="Numeric" Font-Size="8.7pt" Font-Bold="false" AlternatingRowStyle-BackColor="#f4f4f4" 
            HeaderStyle-BackColor="LightGray" AllowPaging="True" ShowFooter="false" PageSize="12" 
            OnPageIndexChanging="gvTables_PageIndexChanging" 
            OnRowDataBound="gvTables_RowDataBound" 
            OnRowCreated="gvTables_RowCreated" 
            OnSelectedIndexChanged="gvTables_SelectedIndexChanged"> 
            <PagerStyle CssClass="pgr" /> 
           </asp:GridView> 

代碼:

private void SetCheckBoxesInGV() 
    { 
     ////add checkboxes in place of active 
     for (int i = 0; i < gvTables.Rows.Count; i++) 
     { 
      System.Web.UI.WebControls.CheckBox chk = new System.Web.UI.WebControls.CheckBox(); 
      chk.ID = "chk"; 
      chk.Checked = gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].Text.Trim() == "y" ? true : false; 
      gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].Controls.Add(chk); 
      gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].ControlStyle.CssClass = "ItemCenter"; 
     } 
    } 

所以最後一列是這樣的,而不是Y/N。

enter image description here

現在我想要得到的複選框值,我用這並認爲它會工作,但事實並非如此。給出空值並將「對象引用未設置爲對象的實例」。

((CheckBox)gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].FindControl("chk")).Checked 

回答

0

所以我所做的是我添加複選框模板領域和使用該link's幫助重新安排列,以便複選框列是最後一列。

然後我能夠很容易地獲得複選框的值。

相關問題