如何从数据库和datagridview中删除

时间:2019-03-27 10:00:35

标签: c# sql

所以我一直在Google周围寻找anwser,以至于发现了许多diffrenet anwsers,但我不太了解它们,即使我已经尝试了很多,也没有真正实现它们的方法,所以我的问题基本上是我的删除按钮仅从datagridview中删除,而不是从数据库本身中删除,我确实将gridview绑定到了我的知识,但这是我制作的第一个表单应用程序,因此我对自己的工作感到有些困惑

    private void buttonDel_Click(object sender, EventArgs e)
    {///////////////////////////////////////////////////////issue is here
        foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
        {
            dataGridView1.Rows.RemoveAt(item.Index);
        }
        //string del = "DELETE FROM Data WHERE RowID = @RowID";
    }
        using (SqlConnection Connection = new SqlConnection(Connectionstring))
        {
            string query = "insert into data(Navn, NummerPlade, KMKørt, dato)";
            query += " values (@Navn, @NummerPlade, @KMKørt, @dato)";
            Connection.Open();
            SqlCommand cmd = new SqlCommand(query, Connection);
            cmd.Parameters.AddWithValue("@Navn", textBox1.Text);
            cmd.Parameters.AddWithValue("@NummerPlade", textBox8.Text);
            cmd.Parameters.AddWithValue("@KMKørt", textBox6.Text);
            cmd.Parameters.AddWithValue("@dato", textBox7.Text);
            cmd.ExecuteNonQuery();
            Connection.Close();
            button4_Click(sender, e);
        }
    private void button4_Click(object sender, EventArgs e)
    {
        /// Connect / Update


        using (SqlConnection Connection = new SqlConnection(Connectionstring))
        {

            Connection.Open();
            SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM  Data", Connection);
            DataTable data = new DataTable();
            sqlDa.Fill(data);
            dataGridView1.DataSource = data;
        }
        buttonConn.Hide();
    }

0 个答案:

没有答案