如何在GridView中一次性批量更新多行数据
2008-12-31 14:55
465 查看
假定有一个Product表,字段有(Id,Name,Quantity,...)我们要一次批量更新Quantity的值
首先在Gridview中,Quantity列以TemplateField显示,其他的列属性设为只读,把显示格式设为TextBox
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow"
Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
</ItemTemplate>
</asp:TemplateField>
在GridView下面添加一个Button控件,定义onclick方法为updateButton_Click
最后updateButton_Click代码为:
protected void updateButton_Click(object sender, EventArgs e)
{
int rowsCount = grid.Rows.Count;
GridViewRow gridRow;
TextBox quantityTextBox;
string productId;
int quantity;
bool success = true;
// 遍历GridView中的每一行
for (int i = 0; i < rowsCount; i++)
{
// 获行当前行
gridRow = grid.Rows[i];
// 通过DATAKEYS来取行没显示出来的ID号
Id = grid.DataKeys[i].Value.ToString();
//
quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
// 转换为整形,如果输入的是非法字符Int32.TryParse返回FALSE
if (Int32.TryParse(quantityTextBox.Text, out quantity))
{
// 调用业务层的方法更新数据
success = success && BLL.UpdateItem(Id, quantity);
}
else
{
// 更新失败
success = false;
}
// 显示信息
statusLabel.Text = success ?
"<br />更新成功!<br />" :
"<br />更新失败!<br />";
}
// 重新绑定GridVIEW
PopulateGridView();
}
资料引用:http://www.knowsky.com/341322.html
首先在Gridview中,Quantity列以TemplateField显示,其他的列属性设为只读,把显示格式设为TextBox
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow"
Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
</ItemTemplate>
</asp:TemplateField>
在GridView下面添加一个Button控件,定义onclick方法为updateButton_Click
最后updateButton_Click代码为:
protected void updateButton_Click(object sender, EventArgs e)
{
int rowsCount = grid.Rows.Count;
GridViewRow gridRow;
TextBox quantityTextBox;
string productId;
int quantity;
bool success = true;
// 遍历GridView中的每一行
for (int i = 0; i < rowsCount; i++)
{
// 获行当前行
gridRow = grid.Rows[i];
// 通过DATAKEYS来取行没显示出来的ID号
Id = grid.DataKeys[i].Value.ToString();
//
quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
// 转换为整形,如果输入的是非法字符Int32.TryParse返回FALSE
if (Int32.TryParse(quantityTextBox.Text, out quantity))
{
// 调用业务层的方法更新数据
success = success && BLL.UpdateItem(Id, quantity);
}
else
{
// 更新失败
success = false;
}
// 显示信息
statusLabel.Text = success ?
"<br />更新成功!<br />" :
"<br />更新失败!<br />";
}
// 重新绑定GridVIEW
PopulateGridView();
}
资料引用:http://www.knowsky.com/341322.html
相关文章推荐
- 如何在GridView中一次性批量更新多行数据
- 如何在GridView中一次性批量更新多行数据
- GridView如何更新批量数据和单条记录(downmoon)?
- GridView如何更新批量数据和单条记录?
- GridView如何更新批量数据和单条记录(downmoon)
- PHP如何批量更新MYSQL中的数据
- mybaits如何批量更新数据库
- GridView布局实现的多行批量更新
- 如何用一个表的数据批量更新另一个表格中的同属性数据
- SQL更新多行数据为不同的值如何实现?
- 对GridView单元格数据进行一次性保存到数据库中 批量保存GridView数据
- 如何批量更新两个相关表的数据???
- 在ASP.NET 2.0中操作数据之六十二:GridView批量更新数据
- 通过GridView布局实现的多行批量更新
- GridView中存在多行数据,如何获取数据表主键值以对某一整行数据进行编辑
- 关于ASP中如何批量数据更新?
- PHP如何批量更新MYSQL中的数据
- 如何把批量数据导入到android 的 sqlite 数据库(更新中)
- ASP.NET DEMO 16: 通过GridView布局实现的多行批量更新
- 在ASP.NET 2.0中操作数据之六十二:GridView批量更新数据