2013-10-28 230 views
14

我有一個GridView,有10列由CheckBoxes填充。但不是使用FindControl()有沒有辦法通過使用循環獲得CheckBox.Checked值?Gridview獲取Checkbox.Checked值

目前代碼:

if (e.CommandName == "updaterow") 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 
    GridViewRow selectedRow = GridView1.Rows[index]; 
    // TableCell BranchCode = selectedRow.Cells[0]; 
    CheckBox cb101 = (CheckBox)selectedRow.FindControl("cb101"); 
    CheckBox cb102 = (CheckBox)selectedRow.FindControl("cb102"); 
    //...and so on 
} 

ASPX CODE:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"> 
    <Columns> 
     <asp:TemplateField HeaderText="101"> 
      <ItemTemplate> 
       <asp:CheckBox runat="server" id="cb101" AutoPostBack="false" Checked='<%# Eval("101").ToString()=="1" ? true : false %>' Enabled='<%#(String.IsNullOrEmpty(Eval("101").ToString()) ? false: true) %>'/> 
      </ItemTemplate> 
     </asp:TemplateField> 
     ....and so on 
     <asp:ButtonField ButtonType="Button" CommandName="updaterow" Text="Update"/> 
    </Columns> 
</asp:GridView> 

回答

17

試試這個,

使用foreach循環:

foreach (GridViewRow row in GridView1.Rows) 
{ 
    CheckBox chk = row.Cells[0].Controls[0] as CheckBox; 
    if (chk != null && chk.Checked) 
    { 
     // ... 
    } 
} 

用它OnRowCommand事件並獲取CheckBox CheckBox值。

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer); 
int requisitionId = Convert.ToInt32(e.CommandArgument); 
CheckBox cbox = (CheckBox)row.Cells[3].Controls[0]; 
0

如果你想比其他的方法FindControl已嘗試以下操作:

GridViewRow row = Gridview1.SelectedRow; 
int CustomerId = int.parse(row.Cells[0].Text);// to get the column value 
CheckBox checkbox1= row.Cells[0].Controls[0] as CheckBox; // you can access the controls like this 
0
 foreach (DataRow row in DataRow row in GridView1.Rows) 
     { 
      foreach (DataColumn c in GridView1.Columns) 

       bool ckbVal = (bool)(row[c.ColumnName]); 

     } 
0

Blockquote

foreach (GridViewRow row in tempGrid.Rows) 
    { 
     dt.Rows.Add(); 
     for (int i = 0; i < row.Controls.Count; i++) 
     { 
      Control control = row.Controls[i]; 
      if (control.Controls.Count==1) 
      { 
       CheckBox chk = row.Cells[i].Controls[0] as CheckBox; 
       if (chk != null && chk.Checked) 
       { 
        dt.Rows[dt.Rows.Count - 1][i] = "True"; 
       } 
       else 
       dt.Rows[dt.Rows.Count - 1][i] = "False"; 
      } 
      else 
       dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text.Replace("&nbsp;", ""); 
     } 
    } 
+0

請一些解釋添加到您的代碼! –

2

對於運行的GridView的所有行不使用for循環,使用foreach循環,如:

foreach (GridViewRow row in yourGridName.Rows) //Running all lines of grid 
{ 
    if (row.RowType == DataControlRowType.DataRow) 
    { 
     CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox); 

     if (chkRow.Checked) 
     { 
       //if checked do something 
     } 
    } 
} 
-1
foreach (GridViewRow row in GridView1.Rows) 
    { 
     CheckBox chkbox = (CheckBox)row.FindControl("CheckBox1"); 
     if (chkbox.Checked == true) 
     { 
      // Your Code 
     } 
    }