我附上代码。我正在尝试删除原始数据并将其附加到带有 datagridview 的原始数据库中。我连续得到了datagridview,但修改没有保存。
我不能让 datagridview 保存任何东西,它只是在下次启动时立即弹出。非常感谢你。
private void button2_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source=MyServerName\\InstanceName;Initial Catalog="+ comboBox1.Text + ";Integrated Security=SSPI;");
string sqlQuery = @"SELECT * from " + comboBox2.Text;
SqlCommand cmd = new SqlCommand(sqlQuery, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
dataGridView1.DataSource = new BindingSource(table, null);
myConnection.Close();
}
private void button3_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
DialogResult question = MessageBox.Show("Are You Sure?", "Please Confirm", MessageBoxButtons.YesNo);
if ( question == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(item.Index);
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
myConnection.Close();
}
else
{
myConnection.Close();
break;
}
}
}
}
}