论坛有不少,何不搜一下?
private void BindDataGrid()
{
SqlConnection myConnection=new SqlConnection();
string strConn="server=Localhost;uid=sa;pwd=123456;database=stucard";
myConnection.ConnectionString=strConn;
SqlDataAdapter myCommand=new SqlDataAdapter("select id,name,sex,pwd from admini",myConnection);
DataSet ds=new DataSet();
myCommand.Fill(ds,"scores");
DataGrid1.DataSource=ds.Tables["scores"].DefaultView;
DataGrid1.DataBind();
}//这个是绑定的。
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindDataGrid();
}//这个是分页的。
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
error();
SqlConnection myConnection=new SqlConnection("server=localhost;uid=sa;pwd=123456;database=stucard");
string strCommand="delete from admini where id='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";
SqlCommand myCommand=new SqlCommand(strCommand,myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
BindDataGrid();//删除后记得绑定。
}//这个是删除的。
你看着办吧。