我附上了代码。 我尝试删除原始数据并使用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;
}
}
}
}
}
答案 0 :(得分:0)
尝试使用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();
}
答案 1 :(得分:0)
从Grid的SelectedRow获取表的主键,并分别在DB表中执行删除操作。
试试这个。
数据库脚本,在运行此脚本之前,请创建名为“TestDB”
的数据库 USE [TestDB]
GO
/****** Object: Table [dbo].[Student] Script Date: 05/24/2013 14:54:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student](
[ID] [int] NOT NULL,
[Name] [nchar](10) NULL,
[Age] [int] NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
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();
}
我希望这能解决你的目的。