绑定过程代码:
private void GridViewToBind()
{
SqlConnection con = new SqlConnection("server=.;database=login;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sdr = new SqlDataAdapter();
sdr.SelectCommand = new SqlCommand("select * from login", con);
DataSet ds = new DataSet();
sdr.Fill(ds, "userName");
this.GridView1.DataKeyNames = new string[] { "userID" };
this.GridView1.DataSource = ds.Tables["userName"].DefaultView;
this.GridView1.DataBind();
}
删除过程代码:
string userID = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();//提取索引
SqlConnection con = new SqlConnection("server=.;database=login;uid=sa;pwd=;");
con.Open();
SqlCommand cmd = new SqlCommand("delete from login where userID='" + userID + "'", con);
cmd.ExecuteNonQuery();
this.GridViewToBind();