删除的时候同时不仅在体现..还删除了对应表的数据
代码:
public partial class Form1 : Form
{
public DataSet ds = new DataSet();
public SqlDataAdapter sdr;
string strSql = "select * from tb";
public SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString.ToString());
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
datasetresource();
}
//更新后保存按钮
private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder scb = new SqlCommandBuilder(sdr);
sdr.Update(ds, "a");
datasetresource();
}
//删除按钮
private void button2_Click(object sender, EventArgs e)
{
int i = this.dataGridView1.SelectedRows.Count;
string strID="";
while (i > 0)
{
strID += this.dataGridView1.SelectedRows[i - 1].Cells[0].Value.ToString()+",";//取出ID主键
this.dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);//删除界面数据
i--;
}
//根据主键删除对应表数据
conn.Open();
string subID=strID.Substring(0,strID.Length-1);
MessageBox.Show(subID.ToString());
SqlCommand comm = new SqlCommand("delete from tb where id in(" + subID.ToString() + ")", conn);
comm.ExecuteNonQuery();
conn.Close();
datasetresource();
}
//datagridview数据源
public void datasetresource()
{
conn.Open();
sdr = new SqlDataAdapter(strSql, conn);
ds.Clear();
sdr.Fill(ds, "a");
conn.Close();
//this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = ds.Tables[0];
}
}