DataGridView选择更改了事件并从数据库中删除了选定的行

时间:2012-05-29 08:08:33

标签: c# sql winforms visual-studio-2010

我的表格上有一个radiobutton,一个gridview和一个按钮。这是我的datagridview selection_changed代码示例:

private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (rdb_Delete.Checked)
    {
        lbl_deleteMsg.Text = DataGridView1.SelectedRows.Count.ToString() 
                + " rows selected.";
    }
 }

我想从数据库中删除选定的行,并在用户按下删除按钮时刷新datagridview。 我的数据库中有一个Id列,但我没有在datagridview上向用户显示。

所以通过sql查询如何从数据库中删除所选行?这是我的删除按钮单击代码,但它似乎不起作用(因为选择更改事件):

private void btn_Delete_Click(object sender, EventArgs e) 
{       
    if (rdb_Delete.Checked)
    {
        int count = DataGridView1.SelectedRows.Count;
        int length = DataGridView1.RowCount;
        if (!count.Equals(0))
        {
            if (confirm())
            {
                for (int i = 0; i < length; i++)
                {
                    if (DataGridView1.Rows[i].Selected)
                    {
                        //DataGridView1.SelectedRows[i].Selected = false;
                        DataGridView1.Rows.RemoveAt(i);
                        i--;
                    }
                }
            }
        }
    }
}

编辑:我也发现一个小答案可能会在这种情况下帮助某人。 DataGridView的任何列都可以是 visible = false ,因此可以通过以下代码访问:

int rowID = int.Parse(DataGridView1[0, selectedIndex].Value.ToString());

3 个答案:

答案 0 :(得分:3)

试试这个:

private void btn_Delete_Click(object sender, EventArgs e) 
{       
    if (rdb_Delete.Checked)
    {
        foreach (DataGridViewRow row in DataGridView1.SelectedRows)
        {
            //delete record and then remove from grid. 
            // you can use your own query but not necessary to use rowid
            int selectedIndex = row.Index;         

            // gets the RowID from the first column in the grid
            int rowID = int.Parse(DataGridView1[0, selectedIndex].Value.ToString());
            string sql = "DELETE FROM Table1 WHERE RowID = @RowID";

            // your code for deleting it from the database
            // then your code for refreshing the DataGridView

            DataGridView1.Rows.Remove(row);
        }
    }
}

答案 1 :(得分:0)

1。)您必须使用双向绑定。 2.)您必须确认更改,以使它们成为非临时性的。

答案 2 :(得分:0)

private void button60_Click(object sender,EventArgs e)         {

        foreach (DataGridViewRow row in dataGridView7.SelectedRows)
        {
            dd = row.Cells["date"].Value.ToString();
            ms = row.Cells["month"].Value.ToString();
            day = row.Cells["day"].Value.ToString();
            string txt = row.Cells["descrip"].Value.ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from holidays where date like '" + dd + "%' and descrip like '"+ txt +"'", con);
            SqlCommand cmd1 = new SqlCommand("delete from stholidays where holiday like '" + dd + "%' and holidescrip like '" + txt + "'", con);
            cmd.ExecuteNonQuery();
            cmd1.ExecuteNonQuery();
            con.Close();
            foreach (DataGridViewCell oneCell in dataGridView7.SelectedCells)
            {

                if (oneCell.Selected)
                    dataGridView7.Rows.RemoveAt(oneCell.RowIndex);

            }
        }