panchengyong

 

在DataGrid快速添加新行(c#)

appe_admin.aspx

<%@ Page language="c#" Codebehind="appe_admin.aspx.cs" AutoEventWireup="false" Inherits="bzh_home.appe_admin" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>appe_admin</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnItemCommand="ItemsGrid_Command"
CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="Employee ID">
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text=''<%# DataBinder.Eval(Container, "DataItem.编号") %>''>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox5 runat="server" Text=''<%# DataBinder.Eval(Container, "DataItem.编号") %>''>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text=''<%# DataBinder.Eval(Container, "DataItem.用户名") %>''>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text=''<%# DataBinder.Eval(Container, "DataItem.密码") %>''>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid>
</form>
</body>
</HTML>


appe_admin.aspx.cs


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace bzh_home
{
/// <summary>
/// appe_admin 的摘要说明。
/// </summary>
public class appe_admin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

string connstr = "user id=sa;data source=\"XIDONGS\\DATAMANAGE\";initial catalog=bzh_data";

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack){
BindGrid();
}

}

private void BindGrid(){
SqlConnection cnn = new SqlConnection(connstr);
SqlDataAdapter da = new SqlDataAdapter("select * from admin", cnn);
DataSet ds = new DataSet();
da.Fill(ds,"admin");

this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();

}

public void ItemsGrid_Command(Object sender, DataGridCommandEventArgs e)
{

if(e.CommandName == "Insert")
{
//this.Page.Response.Write("ss");

SqlConnection cnn = new SqlConnection(connstr);

TextBox t1 = (TextBox)e.Item.FindControl("textbox2");
TextBox t2 = (TextBox)e.Item.FindControl("textbox4");
cnn.Open();
SqlCommand cmd = new SqlCommand("insert into admin(用户名,密码) values(''" + t1.Text + "'',''" + t2.Text + "'')", cnn);
cmd.ExecuteNonQuery();
cnn.Close();
BindGrid();

}


}


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
另一种方法:
效果:
点击DataGrid底部的新建按钮,DataGrid产生一新行,并处于编辑状态,输入并确定后,新数据记录就存储到数据库中。

我的理解:
既然是快速,就不要新建页面,而是在DataGrid中的FooterTemplate中直接加一个LinkButton。在按钮点击事件中为DataTable添加一新行,当然在DataGrid数据绑定的时候,最好缓存DataTable,然后将新行设置为编辑状态,接着DataGrid重新数据绑定。

posted on 2006-03-09 18:01  潘成勇  阅读(2622)  评论(1编辑  收藏  举报

导航