我采用的是重新绑定
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
namespace GridView练习
{
public partial class _Default : System.Web.UI.Page
{
public static OleDbConnection con;
public static DataSet ds = new DataSet();
public static OleDbDataAdapter sda = new OleDbDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ data(); }
}
protected void CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked = ((CheckBox)GridView1.HeaderRow.FindControl("CheckBox2")).Checked;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
{
string s = GridView1.Rows[i].Cells[1].Text.ToString();
OleDbCommand cmd = new OleDbCommand("delete from stu where id='" + s + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ds.Tables.Clear();
data();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string str = "insert into stu values ('" + TextBox1.Text.ToString() + "','" + TextBox2.Text.ToString() +"')";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ds.Tables.Clear();
data();
}
protected void data()
{
string str = "provider=microsoft.jet.oledb.4.0;data source=";
str += Server.MapPath("/") + "db1.mdb";
con = new OleDbConnection(str);
OleDbCommand cmd = new OleDbCommand("select * from stu", con);
sda.SelectCommand = cmd;
sda.Fill(ds, "stu");
GridView1.DataSource = ds.Tables["stu"].DefaultView;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
{
string s = GridView1.Rows[i].Cells[1].Text.ToString();
string str = "update stu set id='" + TextBox1.Text.ToString() + "',stuname='" + TextBox2.Text.ToString() + "' where id='" + s + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch
{ }
ds.Tables.Clear();
data();
}
}
}
数据绑定怎么到页面文件去绑定操作了?!
最好检查下执行步骤,先绑定数据,后赋选择值
不太清楚,帮你关注