1

我附上代码。我正在尝试删除原始数据并将其附加到带有 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;
                }
            }
        }
    }
}
4

2 回答 2

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();
}
于 2013-05-23T10:07:08.690 回答
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();
    }

我希望这能解决你的目的。

于 2013-05-23T10:27:29.197 回答