private void Bind()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter("select * form name",con);
DataSet ds = new DataSet();
sda.Fill(ds,"temp");
con.Close();
GridView1.DataSource = ds.Tables["temp"].DefaultView;
GridView1.DataBind();
}
private void fill(int id,string name,int age)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["ConnStr"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter("select * from name", con);
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
//如果没看上面这句,那DataSet将只能Selete不能Update
DataSet ds = new DataSet();
try
{
sda.Fill(ds,"temp");
ds.Tables["temp"].DefaultView.Sort = "id";
//按id排序
int index = ds.Tables["temp"].DefaultView.Find(id);
//找到我们要的数据所在行的索引
ds.Tables["temp"].Rows[index]["name"] = name;
ds.Tables["temp"].Rows[index]["age"] = age;
//更新DataSet里面的数据必须使用数组的方式。
int rows = sda.Update(ds,"temp");
Response.Write("成功更新了" + rows + "行数据");
}
catch(Exception e)
{
Response.Write("出现错误原因是:" + e.Message);
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
int id = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
//上面是说GridView1的行号要动态取,而列号是固定的
string name = ((TextBox)GridView1.Rows[index].Cells[2].FindControl("TextBox1")).Text;
int age = Convert.ToInt32(((TextBox)GridView1.Rows[index].Cells[3].FindControl("TextBox2")).Text);
fill(id,name,age);
GridView1.EditIndex = -1;
Bind();
}
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter("select * form name",con);
DataSet ds = new DataSet();
sda.Fill(ds,"temp");
con.Close();
GridView1.DataSource = ds.Tables["temp"].DefaultView;
GridView1.DataBind();
}
private void fill(int id,string name,int age)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["ConnStr"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter("select * from name", con);
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
//如果没看上面这句,那DataSet将只能Selete不能Update
DataSet ds = new DataSet();
try
{
sda.Fill(ds,"temp");
ds.Tables["temp"].DefaultView.Sort = "id";
//按id排序
int index = ds.Tables["temp"].DefaultView.Find(id);
//找到我们要的数据所在行的索引
ds.Tables["temp"].Rows[index]["name"] = name;
ds.Tables["temp"].Rows[index]["age"] = age;
//更新DataSet里面的数据必须使用数组的方式。
int rows = sda.Update(ds,"temp");
Response.Write("成功更新了" + rows + "行数据");
}
catch(Exception e)
{
Response.Write("出现错误原因是:" + e.Message);
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
int id = Convert.ToInt32(GridView1.Rows[index].Cells[1].Text);
//上面是说GridView1的行号要动态取,而列号是固定的
string name = ((TextBox)GridView1.Rows[index].Cells[2].FindControl("TextBox1")).Text;
int age = Convert.ToInt32(((TextBox)GridView1.Rows[index].Cells[3].FindControl("TextBox2")).Text);
fill(id,name,age);
GridView1.EditIndex = -1;
Bind();
}