Suppose there is a Product table with fields (Id, Name, Quantity,...) and we want to update the Quantity value in batches at one time
First, in the Gridview, the Quantity column is displayed as TemplateField, the other column properties are set to read-only, and the display format is set to TextBox
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow"
Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
</ItemTemplate>
</asp:TemplateField>
Add a Button control below the GridView and define the onclick method as updateButton_Click
The final updateButton_Click code is:
protected void updateButton_Click(object sender, EventArgs e)
{
int rowsCount = grid.Rows.Count;
GridViewRow gridRow;
TextBox quantityTextBox;
string productId;
int quantity;
bool success = true;
// Traverse each row in the GridView
for (int i = 0; i < rowsCount; i++)
{
// Get the current row
gridRow = grid.Rows[i];
// Use DATAKEYS to get the ID number that is not displayed
Id = grid.DataKeys[i].Value.ToString();
//
quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
// Convert to integer, if the input is an illegal character Int32.TryParse returns FALSE
if (Int32.TryParse(quantityTextBox.Text, out quantity))
{
// Call the business layer method to update the data
success = success && BLL.UpdateItem(Id, quantity);
}
else
{
//Update failed
success = false;
}
// display information
statusLabel.Text = success?
"<br />Update successful!<br />" :
"<br />Update failed!<br />";
}
//Rebind GridVIEW
PopulateGridView();
}
http://www.cnblogs.com/timone/archive/2006/11/17/564171.html