using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace MikeCat
{
/// <summary>
    /// </summary>
public class MikeCat_DataGrid_ADM : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.DataGrid mfdg;
protected System.Web.UI.WebControls.TextBox FN1;
protected System.Web.UI.WebControls.TextBox LN1;
protected System.Web.UI.WebControls.Button Cancel1;
protected System.Web.UI.WebControls.Button Add1;

String sql ="Select * FROM 临时人员";
String strCnn = "Server=(local);User id=sa;Pwd=21search;Database=北风贸易"; public ICollection CreateDataSource () 
{
SqlConnection conn = new SqlConnection(strCnn); SqlDataAdapter da = new SqlDataAdapter(sql,conn); DataSet ds = new DataSet();
da.Fill(ds,"mfData"); DataView myView = ds.Tables["mfData"].DefaultView;
return myView;
} public void Page_Load(Object sender, EventArgs e) 
{
if (!IsPostBack) 
{

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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Cancel1.Click += new System.EventHandler(this.Cancel1_Click);
this.mfdg.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.mfdg_PageIndexChanged);
this.mfdg.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.mfdg_CancelCommand);
this.mfdg.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.mfdg_EditCommand);
this.mfdg.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.mfdg_UpdateCommand);
this.mfdg.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.mfdg_DeleteCommand);
this.Add1.Click += new System.EventHandler(this.Add1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void mfdg_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{

mfdg.CurrentPageIndex = e.NewPageIndex;
mfdg.DataSource = CreateDataSource();
mfdg.DataBind();
} private void mfdg_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
mfdg.EditItemIndex = e.Item.ItemIndex;
mfdg.DataSource = CreateDataSource();
mfdg.DataBind();
} private void mfdg_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

mfdg.EditItemIndex = -1;
mfdg.DataSource = CreateDataSource();
mfdg.DataBind();
} private void mfdg_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string FirstName = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string LastName = ((TextBox)e.Item.Cells[2].Controls[1]).Text; SqlConnection connUpdate = new SqlConnection(strCnn);
connUpdate.Open();
String sql_edit = "UPDATE 临时人员 SET 员工姓名 = '" + FirstName.Replace("'","''")+ "',家庭住址 = '" + LastName.Replace("'","''")+ "' WHERE 身份证号码 = " + e.Item.Cells[0].Text;
SqlCommand SqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
SqlCommandUpdate.ExecuteNonQuery();
connUpdate.Close();
mfdg.EditItemIndex = -1;
mfdg.DataSource = CreateDataSource();
mfdg.DataBind();
} private void mfdg_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection connDel = new SqlConnection(strCnn);
connDel.Open();
String sql_Del = "DELETE FROM 临时人员 WHERE 身份证号码 = " + e.Item.Cells[0].Text; SqlCommand SqlCommandDel = new SqlCommand(sql_Del,connDel);
SqlCommandDel.ExecuteNonQuery();
connDel.Close();
mfdg.EditItemIndex = -1;
mfdg.DataSource = CreateDataSource();
mfdg.DataBind();
} private void Add1_Click(object sender, System.EventArgs e)
{
SqlConnection connAdd = new SqlConnection(strCnn);
connAdd.Open();
String sql_Add = "insert into 临时人员(员工姓名,家庭住址) values('"+FN1.Text.Trim().Replace("'","''")+"','"+LN1.Text.Trim().Replace("'","''")+"')"; SqlCommand SqlCommandAdd = new SqlCommand(sql_Add,connAdd);
SqlCommandAdd.ExecuteNonQuery();
connAdd.Close(); mfdg.DataSource = CreateDataSource();
mfdg.DataBind();
} private void Cancel1_Click(object sender, System.EventArgs e)
{
FN1.Text="";
LN1.Text="";
}
}
}

解决方案 »

  1.   

    <%@ Page language="c#" Codebehind="MikeCat_DataGrid_ADM.aspx.cs" AutoEventWireup="false" Inherits="MikeCat.MikeCat_DataGrid_ADM" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>MikeCat_DataGrid_ADM</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 runat="server" ID="Form1">
    <font face="arial" size="3"><b>
    <asp:TextBox id="FN1" style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 48px" runat="server"
    BorderWidth="1px" BorderColor="RoyalBlue" BackColor="Lavender" MaxLength="50" tabIndex="1"></asp:TextBox>
    <asp:Button id="Cancel1" style="Z-INDEX: 107; LEFT: 600px; POSITION: absolute; TOP: 48px" runat="server"
    BorderWidth="1px" Width="50px" BorderColor="RoyalBlue" BackColor="Lavender" Font-Names="Arial"
    Font-Size="8pt" Text="Cancel" tabIndex="4"></asp:Button>
    <asp:TextBox id="LN1" style="Z-INDEX: 106; LEFT: 368px; POSITION: absolute; TOP: 48px" runat="server"
    BorderWidth="1px" BorderColor="RoyalBlue" BackColor="Lavender" MaxLength="50" tabIndex="2"></asp:TextBox>
    <asp:Label id="Label4" style="Z-INDEX: 104; LEFT: 56px; POSITION: absolute; TOP: 48px" runat="server"
    Font-Names="Arial" Font-Size="8pt" Font-Bold="True">First Name:</asp:Label>可添加,编辑,删除的DATAGRID--欢迎光临老猫的理想</b>
    </font>
    <br>
    <asp:DataGrid id="mfdg" runat="server" BorderColor="RoyalBlue" Width="640px" AllowPaging="True"
    BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False" style="Z-INDEX: 100; LEFT: 56px; POSITION: absolute; TOP: 80px">
    <EditItemStyle Font-Size="7pt" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Black" BackColor="ScrollBar"></EditItemStyle>
    <AlternatingItemStyle Font-Size="8pt" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Black" BackColor="White"></AlternatingItemStyle>
    <ItemStyle Font-Size="8pt" Font-Names="Arial" HorizontalAlign="Center" ForeColor="Black" BackColor="Lavender"></ItemStyle>
    <HeaderStyle Font-Size="9pt" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Center" ForeColor="White"
    BorderColor="White" BackColor="DarkSlateBlue"></HeaderStyle>
    <Columns>
    <asp:BoundColumn DataField="身份证号码" ReadOnly="True" HeaderText="ID" ItemStyle-Font-Size="12px"></asp:BoundColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <B>First Name </B>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "员工姓名").ToString().Trim() %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id=FirstName runat="server" Width="100%" Text='<%# DataBinder.Eval(Container.DataItem, "员工姓名").ToString().Trim() %>' MaxLength="50">
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <B>Last Name </B>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "家庭住址").ToString().Trim() %>' Width="200">
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id=LastName runat="server" MaxLength="50" Text='<%# DataBinder.Eval(Container.DataItem, "家庭住址").ToString().Trim() %>' Width="100%">
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑" ItemStyle-Font-Size="12px"></asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete" ItemStyle-Font-Size="12px"></asp:ButtonColumn>
    </Columns>
    <PagerStyle Font-Size="8pt" Mode="NumericPages"></PagerStyle>
    </asp:DataGrid>
    <asp:Button id="Add1" style="Z-INDEX: 103; LEFT: 536px; POSITION: absolute; TOP: 48px" runat="server"
    Text="Add" BorderWidth="1px" Width="50px" BorderColor="RoyalBlue" BackColor="Lavender" Font-Names="Arial"
    Font-Size="8pt" tabIndex="3"></asp:Button>
    <asp:Label id="Label3" style="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 48px" runat="server"
    Font-Bold="True" Font-Names="Arial" Font-Size="8pt">Last Name:</asp:Label>
    </form>
    </body>
    </HTML>
      

  2.   

    谢谢上面这个很长的帖子!look