The original Published time: 2008-08-01--from my Baidu article [imported by moving tools]
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
GridView No data source implementation update (break update) Drag and drop a GridView when you do not edit this item, you should change its properties Autogeneraltededitbutton this property to true, so that editing this link will appear
public partial class Default3:System.Web.UI.Page
{
SqlConnection conn = new SqlConnection ("server=.\\sqlexpress;database=test;uid=sa;pwd=123456");
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
Bind ();
}
private void Bind ()
{
SqlDataAdapter SDA = new SqlDataAdapter ("select * from Stu", Conn);
DataSet ds = new DataSet ();
SDA. Fill (ds, "temp");
Gridview1.datasource = ds. tables["Temp"]. DefaultView;
Gridview1.databind ();
}
private void fill (int id, string name, String Banji)
{
SqlDataAdapter SDA = new SqlDataAdapter ("select * from Stu", Conn);
SqlCommandBuilder scbld = new SqlCommandBuilder (SDA);//Because there is this sentence, So when you close the connection, you can also update the
DataSet ds = new DataSet ();
Sda. Fill (ds, "temp");
Ds. tables["Temp"]. Defaultview.sort = "id";
int index = ds. tables["Temp"]. Defaultview.find (ID);
Ds. tables["Temp"]. rows[index]["name"] = name;
Ds. tables["Temp"]. Rows[index]["class"] = Banji;
int rows = SDA. Update (ds, "temp");
Response.Write ("Update" + Rows + "rows datas");
}
protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)
{
int index = E.rowindex;
int id = Convert.ToInt32 (gridview1.rows[index]. CELLS[1]. Text);
String name = ((TextBox) Gridview1.rows[index]. CELLS[2]. FindControl ("TextBox1")). Text;
String Banji = ((TextBox) Gridview1.rows[index]. CELLS[3]. FindControl ("TextBox2")). Text;
Fill (ID, name, Banji);
Gridview1.editindex =-1;
Bind ();
}
protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e)
{
Gridview1.editindex = E.neweditindex;
Bind ();
}
protected void Gridview1_rowcancelingedit (object sender, Gridviewcancelediteventargs e)
{
Gridview1.editindex =-1;
Bind ();
}
}
GridView No data source implementation update database (i.e. disconnect update database)