GridView实现多行编辑
前台页面
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridMuliEdit.aspx.cs" Inherits="GridMuliEdit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="添加空行" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="保存所有" OnClick="Button2_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:TemplateField HeaderText="产品">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "产品")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量"> <ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "数量")%>'></asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="日期"> <ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "日期")%>'></asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "状态")%>'>
<asp:ListItem Value="1">有效</asp:ListItem>
<asp:ListItem Value="0">无效</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="添加空行" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="保存所有" OnClick="Button2_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:TemplateField HeaderText="产品">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "产品")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量"> <ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "数量")%>'></asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="日期"> <ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "日期")%>'></asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem, "状态")%>'>
<asp:ListItem Value="1">有效</asp:ListItem>
<asp:ListItem Value="0">无效</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
后台代码:
代码
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class GridMuliEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTestData();
}
}
protected void BindTestData()
{
//模拟出一些原始数据绑定DataGrid
DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("产品");
dt1.Columns.Add("数量");
dt1.Columns.Add("日期");
dt1.Columns.Add("状态");
dt1.Rows.Add(new object[] { 123, "产品AA", 12, "2006-11-14", "1" });
dt1.Rows.Add(new object[] { 124, "产品BB", 21, "2006-11-13", "0" });
dt1.AcceptChanges();
this.GridView1.DataSource = dt1;
this.GridView1.DataBind();
}
protected DataTable GetDataFromGrid()
{
DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("产品");
dt1.Columns.Add("数量");
dt1.Columns.Add("日期");
dt1.Columns.Add("状态");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gRow = GridView1.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] = GridView1.DataKeys[i].Value;
newRow[1] = ((TextBox)gRow.FindControl("TextBox1")).Text;
newRow[2] = ((TextBox)gRow.FindControl("TextBox2")).Text;
newRow[3] = ((TextBox)gRow.FindControl("TextBox3")).Text;
newRow[4] = ((DropDownList)gRow.FindControl("DropDownList1")).SelectedValue;
dt1.Rows.Add(newRow);
}
dt1.AcceptChanges();
return dt1;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
DataRow newRow = dt.NewRow();
newRow["状态"] = "1";
dt.Rows.Add(newRow);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
foreach (DataRow row in dt.Rows)
{
if (row["ID"] != null)
{
//更新该行记录到数据库
}
else
{
//插入该行记录到数据库
}
}
}
}
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class GridMuliEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTestData();
}
}
protected void BindTestData()
{
//模拟出一些原始数据绑定DataGrid
DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("产品");
dt1.Columns.Add("数量");
dt1.Columns.Add("日期");
dt1.Columns.Add("状态");
dt1.Rows.Add(new object[] { 123, "产品AA", 12, "2006-11-14", "1" });
dt1.Rows.Add(new object[] { 124, "产品BB", 21, "2006-11-13", "0" });
dt1.AcceptChanges();
this.GridView1.DataSource = dt1;
this.GridView1.DataBind();
}
protected DataTable GetDataFromGrid()
{
DataTable dt1 = new DataTable("Table1");
dt1.Columns.Add("ID");
dt1.Columns.Add("产品");
dt1.Columns.Add("数量");
dt1.Columns.Add("日期");
dt1.Columns.Add("状态");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gRow = GridView1.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] = GridView1.DataKeys[i].Value;
newRow[1] = ((TextBox)gRow.FindControl("TextBox1")).Text;
newRow[2] = ((TextBox)gRow.FindControl("TextBox2")).Text;
newRow[3] = ((TextBox)gRow.FindControl("TextBox3")).Text;
newRow[4] = ((DropDownList)gRow.FindControl("DropDownList1")).SelectedValue;
dt1.Rows.Add(newRow);
}
dt1.AcceptChanges();
return dt1;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
DataRow newRow = dt.NewRow();
newRow["状态"] = "1";
dt.Rows.Add(newRow);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
foreach (DataRow row in dt.Rows)
{
if (row["ID"] != null)
{
//更新该行记录到数据库
}
else
{
//插入该行记录到数据库
}
}
}
}