转:点击按钮添加行

前台代码

 
  
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " GridMuliEdit.aspx.cs " Inherits = " CSDN_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 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 CSDN_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
{
// 插入该行记录到数据库
}
}
}
}

转载于:https://www.cnblogs.com/diony/archive/2011/05/05/2038135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值