2011-08-15 38 views
0

我似乎无法获取gridview中单元格的值以用于存储过程。这里是我的aspx:需要在gridview中获取单元格的值

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" SkinID="GridView" DataKeyNames="Check Config"> 
      <Columns> 
       <asp:BoundField HeaderText="Config ID" DataField="Check Config"></asp:BoundField> 
       <asp:BoundField HeaderText="Check Configuration" DataField="Check Configuration" /> 
       <asp:BoundField HeaderText="Shift" DataField="Shift" /> 
       <asp:BoundField HeaderText="Earliest Time" DataField="Earliest Time" /> 
       <asp:BoundField HeaderText="Alarm Time" DataField="Alarm Date" /> 
       <asp:BoundField HeaderText="Disposition" DataField="Disposition" /> 
       <asp:TemplateField HeaderText="Disable"> 
        <ItemTemplate> 
         <asp:CheckBox runat="server" ID="RowLevelCheckBox" /> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

这里是我的代码隐藏其中,我试图让GridView中第一列/单元格的值存储过程的参数。在这种情况下,我期待获得“Config ID”的值,但每次都返回0。自从我回到ASP.NET之后已经有一段时间了,但是这个项目需要它。

try 
     { 
      foreach (GridViewRow dr in gvData.Rows) 
      { 
       CheckBox chk = (CheckBox)dr.Cells[6].FindControl("RowLevelCheckBox"); 
       if (chk.Checked) 
       { 
        recordCount += 1; 
        int theConfigID = Convert.ToInt32(dr.Cells[0].FindControl("Config ID").ToString()); 
        //cancel these alarms in DB 
        command.Parameters.Add(new SqlParameter("@CHECK_SCHEDULE_ID", theConfigID)); 
        command.ExecuteNonQuery(); 

        //return status and msg 
        lblStatusMessage.ForeColor = System.Drawing.Color.DarkGreen; 
        lblStatusMessage.Text = string.Format("{0} alarm(s) were successfully cancelled.", recordCount); 
        lblStatusMessage.Visible = true; 
       } 
      } 
     } 
+0

gridview渲染与你想要获取的值? – Martin

+0

是的,gridview准确地显示了第一列的数据 – Matt

回答

1

刚刚dr.Cells [0] .Text怎么样?

0

我建议不要在列名中使用空格,并使用gridview上的DataKeyNames属性来访问该ID。这样你甚至不必把ID显示给用户。

foreach(GridViewRow dr in gvData.Rows) 
{ 
    CheckBox chk = (CheckBox)dr.Cells[6].FindControl("RowLevelCheckBox"); 
    if (chk.Checked) 
    { 
     recordCount += 1; 
     int theConfigID = (int) gvData.DataKeys[dr.RowIndex].Value; 
     //cancel these alarms in DB 
     command.Parameters.Add(new SqlParameter("@CHECK_SCHEDULE_ID", theConfigID)); 
     command.ExecuteNonQuery(); 

     //return status and msg 
     lblStatusMessage.ForeColor = System.Drawing.Color.DarkGreen; 
     lblStatusMessage.Text = string.Format("{0} alarm(s) were successfully cancelled.", recordCount); 
     lblStatusMessage.Visible = true; 
    } 
}