C# DataGridView不应用更改
我正在附上密码。 我正在尝试删除一个原始数据,并使用datagridview将其附加到原始数据库中。 我成功地获得了datagridview,但是修改没有保存 我不能让datagridview保存任何内容,它只会在下次发布时弹出。 事先非常感谢你C# DataGridView不应用更改,c#,C#,我正在附上密码。 我正在尝试删除一个原始数据,并使用datagridview将其附加到原始数据库中。 我成功地获得了datagridview,但是修改没有保存 我不能让datagridview保存任何内容,它只会在下次发布时弹出。 事先非常感谢你 private void button2_Click(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("Data Sour
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;
}
}
}
}
}
尝试使用dataGridView1.Bind
代码应为:
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);
dataGridView1.DataBind();
myConnection.Close();
}
从网格的SelectedRow中获取表的主键,并分别在DB table中执行删除操作 试试这个 DB脚本,在运行该脚本之前,创建一个名为TestDB的DB C代码 用于网格绑定
private void button2_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source=.\\;Initial Catalog=TestDB;Integrated Security=SSPI;");
string sqlQuery = @"SELECT * from Student" ;
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)
{
MessageBox.Show(item.Cells["ID"].Value.ToString());
DeleteFromTable(Convert .ToInt32 (item.Cells["ID"].Value));
//dataGridView1.Rows.RemoveAt(item.Index);
//dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
else
{
break;
}
}
}
public void DeleteFromTable(int primaryKey)
{
SqlConnection myConnection = new SqlConnection("Data Source=.\\;Initial Catalog=TestDB;Integrated Security=SSPI;");
string sqlQuery = @"DELETE FROM Student WHERE ID = " + primaryKey + "(SELECT * from Student)";
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();
}
我希望这能解决您的问题。他的代码似乎是winforms。DataGridViews没有数据绑定方法。删除数据库中的行的代码在哪里,您展示的是如何从DataGridViews中删除这些行。您能举个例子吗?您能详细说明一下吗?可以举个例子吗?
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)
{
MessageBox.Show(item.Cells["ID"].Value.ToString());
DeleteFromTable(Convert .ToInt32 (item.Cells["ID"].Value));
//dataGridView1.Rows.RemoveAt(item.Index);
//dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
else
{
break;
}
}
}
public void DeleteFromTable(int primaryKey)
{
SqlConnection myConnection = new SqlConnection("Data Source=.\\;Initial Catalog=TestDB;Integrated Security=SSPI;");
string sqlQuery = @"DELETE FROM Student WHERE ID = " + primaryKey + "(SELECT * from Student)";
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();
}