我试图在代码隐藏中获取BoundField的值以更新到SQL服务器,但它不断给我旧的值。
HTML:
<asp:GridView ID="gridviewtxSLds" ClientIDMode="Static" runat="server" NullDisplayText="" DataKeyNames="item,AD,BD" CssClass="gridclassscrolledtx" CellPadding="0" ForeColor="#333333" OnRowCreated="gridviewtxSLds_RowCreated" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="item" ReadOnly="false" ItemStyle-CssClass="m2" HeaderText="Metric" />
<asp:BoundField DataField="AD" ReadOnly="false" ItemStyle-CssClass="txttime" HeaderText="AD" />
<asp:BoundField DataField="BD" ReadOnly="false" ItemStyle-CssClass="txttime" HeaderText="BD" />
</Columns>
</asp:GridView>
点击定时按钮会触发以下功能:
protected void Bulk_Insert(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < gridviewtxSLds.Rows.Count; i++)
{
#region textbox declarations
string txtitem = gridviewtxSLds.DataKeys[i].Values["item"].ToString();
string txtAD = gridviewtxSLds.DataKeys[i].Values["AD"].ToString();
string txtBD = gridviewtxSLds.DataKeys[i].Values["BD"].ToString();
#endregion
#region command string
string s = "update D" + timeinitid + "tx set AD";
s += "='";
s += txtAD.Trim();
s += "', ";
s += "BD";
s += "='";
s += txtBD.Trim();
s += "' where item";
s += "='";
s += txtitem.Trim();
s += "';";
#endregion
sb.Append(s.ToString());
}
SqlCommand cmd = new SqlCommand(sb.ToString(), con);
con.Open();
cmd.ExecuteNonQuery();
}
如果我更改gridview中的值,它仍会将旧值发送到数据库。知道为什么吗?