我似乎无法在gridview中获取单元格的值以在存储过程中使用。这是我的aspx:
<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中第一列/单元格的值以获取存储过程参数。在这种情况下,我希望获得“配置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 :(得分:1)
怎么样只是dr.Cells [0] .Text?
答案 1 :(得分: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;
}
}