2

我的表单上有一个单选按钮、一个网格视图和一个按钮。这是我的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 的任何列都可以是可见的 = false,因此可以通过以下代码访问它:

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

3 回答 3

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);
        }
    }
}
于 2012-05-29T08:16:20.137 回答
0

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

于 2012-05-29T08:15:42.773 回答
0

私人无效按钮60_Click(对象发送者,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);

            }
        }
于 2013-02-26T09:52:26.297 回答