谁说的?不是的:前台:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="datagrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title> 
<!-- #include virtual="common_function.inc" -->
<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" bgColor="#339999">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 176px"
runat="server" Width="344px" Height="264px" AutoGenerateColumns="False" DataKeyField="dept_id">
<Columns>
<asp:EditCommandColumn EditText="修改" CancelText="放弃" UpdateText="保存" ItemStyle-Wrap="False" ButtonType="PushButton" />
<asp:ButtonColumn Text="删除" CommandName="Delete" ButtonType="PushButton" />
<asp:TemplateColumn HeaderText="部门代码">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "dept_id") %>' ID="Label3"/>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="部门名称">
    <ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "dept_name") %>' ID="Label4"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="edit_dept_name" Text='<%# DataBinder.Eval(Container.DataItem, "dept_name") %>'/>
</EditItemTemplate>
</asp:TemplateColumn> 
</Columns>
</asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 96px" runat="server"
Width="136px" Height="24px">部门代码:</asp:Label>
<asp:TextBox id="dept_id" style="Z-INDEX: 103; LEFT: 112px; POSITION: absolute; TOP: 88px" runat="server"
Width="80px" Height="32px"></asp:TextBox>
<asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 224px; POSITION: absolute; TOP: 96px" runat="server"
Width="96px" Height="32px">部门名称:</asp:Label>
<asp:TextBox id="dept_name" style="Z-INDEX: 105; LEFT: 312px; POSITION: absolute; TOP: 88px"
runat="server" Width="90px" Height="34px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 432px; POSITION: absolute; TOP: 88px" runat="server"
Width="240px" Height="32px" Text="添加" Font-Bold="True"></asp:Button></FONT>
</form>
</body>
</HTML>

解决方案 »

  1.   

    后台: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;
    using System.Configuration;  namespace datagrid
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    private string ConnectionString = ConfigurationSettings.AppSettings["sqlconn"];
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.TextBox dept_id;
    protected System.Web.UI.WebControls.TextBox dept_name;
    private SqlConnection conn = null; 

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!Page.IsPostBack)
    MyDataBind();
    }
    private void MyDataBind()
    {
    conn = new SqlConnection(ConnectionString);
    if( conn.State == ConnectionState.Open)
    conn.Close();
    ConnectionString =ConfigurationSettings.AppSettings["sqlconn"];
    conn.ConnectionString = ConnectionString;
    conn.Open();  SqlDataAdapter da = new SqlDataAdapter("SELECT * from 部门表" , conn);  
    DataSet ds = new DataSet("myTable");
    da.Fill(ds, "myTable");
    //如果找到的话
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind(); 

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

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    string insertcmd="insert into 部门表 values(@dept_id,@dept_name)";
    SqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"] ) ;
    SqlCommand myCommand=new SqlCommand(insertcmd,myConnection);
    myCommand.Parameters.Add(new SqlParameter("@dept_id",SqlDbType.VarChar ,50));
    myCommand.Parameters["@dept_id"].Value=dept_id.Text.Trim();
    myCommand.Parameters.Add(new SqlParameter("@dept_name",SqlDbType.VarChar ,50));
    myCommand.Parameters["@dept_name"].Value=dept_name.Text.Trim();
    myConnection.Open();
    try
    {
    myCommand.ExecuteNonQuery();
    }
    catch
    {
    Response.Write("err!");
    }
    myConnection.Close();
    MyDataBind();
       
    } private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    } private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string deletecmd="delete from 部门表 where dept_id=@dept_id";
    SqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"] ) ;
    SqlCommand myCommand=new SqlCommand(deletecmd,myConnection);
    myCommand.Parameters.Add(new SqlParameter("@dept_id",SqlDbType.VarChar ,50));
    myCommand.Parameters["@dept_id"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];
    myConnection.Open();
    myCommand.ExecuteNonQuery(); 
                MyDataBind();  
    } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try
    {
    DataGrid1.EditItemIndex=e.Item.ItemIndex;
    MyDataBind();
    }
    catch(System.Exception errr)
    {
    Response.Write(errr.ToString() ); 
    }
    } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try
    {
    DataGrid1.EditItemIndex=-1;
    MyDataBind();
    }
    catch(System.Exception err)
    {
    Response.Write(err.ToString() ); 
    }
    } private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try 
    {
    string updateCmd = "update 部门表 set dept_name=@dept_name where  dept_id=@dept_id";
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"]);
    SqlCommand  myCommand = new SqlCommand(updateCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@dept_name", SqlDbType.VarChar, 50));
    myCommand.Parameters["@dept_name"].Value=((TextBox)e.Item.FindControl("edit_dept_name")).Text;   
      
    myCommand.Parameters.Add(new SqlParameter("@dept_id", SqlDbType.VarChar, 50));
    myCommand.Parameters["@dept_id"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString() ;   
    myConnection.Open(); try 
    {
    myCommand.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException err2) 
    {
    Response.Write(err2.ToString() ); 
    }
    myConnection.Close();
    //重新绑定数据
    MyDataBind();
    //恢复到正常模式
    DataGrid1_CancelCommand(source,e);

     
    }
    catch (System.Exception err)
    {
    Response.Write(err.ToString() ); 
    }
    } }
    }
      

  2.   

    你也可以放到cs里面!如果你使用的是VS.NET这一切都是自然而然的。否则可能要麻烦一些!
    建议你使用VS.NET!你说的那些都在DataGrid的事件(属性面版)里面!