string con = WebConfigurationManager.ConnectionStrings["insystemConnectionString"].ConnectionString;
SqlConnection conn;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//DataSet和GridView绑定
public void bind()
{
conn = new SqlConnection(con);
conn.Open();
SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from jz", conn);
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "jz");
GridView1.DataSource = thisDataSet.Tables["jz"];
GridView1.DataBind();
GridView1.DataKeyNames = new string[] { "szd" };
conn.Close();
}
//编辑按钮
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
} //以下为删除按钮事件
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strsql = "delete from jz where szd=" + GridView1.DataKeys[e.RowIndex].Value.ToString();
conn = new SqlConnection(con);
com = new SqlCommand(strsql, conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
你设置过没有DataKeyNames呢?如过又的话使用(TextBox)this.GridView1.Rows[e.RowIndex].Cells[<列数 整型>].Controls[0]).Text 尝试一下!
我QQ是 383257357 我以前做过已个例子!我可以传给你!
DataKeyNames=new string[]{"绑定的列名"}
DataKeyNames,需位主键,要标志这一行
我知道是哪里的错,兄弟我也遇到过!你不要用gridview自带的那个分页功能再试试,绝对没问题的!你可以用其他的方法分页,有很多好用的方法,就是别在footer上分页,因为删除功能只能搜索到本页的东西,翻页之后就不行了!而且我觉得啊,它自带的那个分页很不美观你觉得呢!网上有很多这样的分页功能!随便弄个就行了!找不到的话跟我说,我给你写个!呵呵!