test.aspx.cs:
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;  
using System.IO;
 
namespace Datagrid1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected string ConnectionString =ConfigurationSettings.AppSettings["sqlconn"];
    protected DataTable dt;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected static string pname="";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
MyDataBind();
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}
private void MyDataBind()
{
pname="标准直浇口系列";
string s=ConfigurationSettings.AppSettings["sqlconn"];;
SqlConnection cn=new SqlConnection(s);
string procedureString="pname";
SqlCommand cd=cn.CreateCommand();
cd.CommandText=procedureString;
cd.CommandType=CommandType.StoredProcedure;
cd.Parameters.Add(new SqlParameter("@pname", SqlDbType.VarChar, 50));
cd.Parameters["@pname"].Value=pname.Trim();
cn.Open();
cd.ExecuteNonQuery();
SqlDataAdapter dr=new SqlDataAdapter();
dr.SelectCommand=cd;
DataSet ds=new DataSet();
dr.Fill(ds,"product");
dt=ds.Tables["product"];
cn.Close();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
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.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

string deletecmd="delete from product where Name=@Name";
SqlConnection myConnection =new SqlConnection(ConnectionString ) ;
SqlCommand myCommand=new SqlCommand(deletecmd,myConnection);
myCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar ,50));
myCommand.Parameters["@Name"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];
myConnection.Open();
myCommand.ExecuteNonQuery(); 
MyDataBind();
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
MyDataBind();
DataGrid1.DataSource=dt;
    DataGrid1.DataBind();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
DataGrid1.EditItemIndex=-1;
MyDataBind();
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
catch(System.Exception err)
{
Response.Write(err.ToString() ); 
}
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string updateCmd = "update product set Catagroy=@Catagroy,Catlog=@Catlog, System1=@System1 where Name=@Name";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand  myCommand = new SqlCommand(updateCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@Catagroy", SqlDbType.VarChar, 50));
myCommand.Parameters["@Catagroy"].Value=((TextBox)e.Item.FindControl("edit_Catagroy")).Text; myCommand.Parameters.Add(new SqlParameter("@Catlog", SqlDbType.VarChar, 50));
myCommand.Parameters["@Catlog"].Value=((TextBox)e.Item.FindControl("edit_Catlog")).Text;   
  
myCommand.Parameters.Add(new SqlParameter("@System1", SqlDbType.VarChar, 50));
myCommand.Parameters["@System1"].Value=((TextBox)e.Item.FindControl("edit_System1")).Text;    myCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 50));
myCommand.Parameters["@Name"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString() ;   
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
//重新绑定数据
MyDataBind();
//恢复到正常模式
DataGrid1_CancelCommand(source,e);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
} private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//startIndex=e.NewPageIndex*DataGrid1.PageSize;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
} }
}

解决方案 »

  1.   

    这样的代码,datagrid能够进行修改,但是不能分页.
    但如果我把if(!this.IsPostBack)去掉,却能分页,但不能修改,我搞的头都是大的了,还没搞定,有谁能帮帮我呀
      

  2.   

    看不懂你的分页代码,分页后要重新绑定啊
    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    //startIndex=e.NewPageIndex*DataGrid1.PageSize;
                                 DataGrid1.CurrentPageIndex = e.NewPageIndex;
                               MyDataBind();
    DataGrid1.DataSource=dt;
    DataGrid1.DataBind();
    }
      

  3.   

    你在private void Page_Load(object sender, System.EventArgs e)里面不要绑定,你不如把 MyDataBind();
    DataGrid1.DataSource=dt;
    DataGrid1.DataBind();
    放道datagrid的初始化状态中,显示的时候,就调用datagrid的初始状态,这样看看
      

  4.   

    谢谢: laser0126(风铃草)
    解决了.这20分都给你,
    还不知称你为兄弟呢,还是姑娘.呵呵