2011-08-15 58 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; 
    } 
} 
相關問題