2012-05-29 136 views
2

我在窗体上有一个单选按钮,一个gridview和一个按钮。这里是我的datagridview selection_changed代码示例:DataGridView选择更改事件并从数据库中删除选定的行

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中的任何一列可以可见=假所以它会被程序代码所访问,如:

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

看看这里http://stackoverflow.com/questions/5539243/how -to-delete-a-selected-row-from-datagridview-and-database它可能会帮助你.. –

回答

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); 
     } 
    } 
} 
+0

我试试这个。它几乎可以,但我的datagridview上没有ID列(在我的数据库上,但没有在dgv上),所以它给我一个解析错误。有没有其他方法可以从数据库中删除行? – pikk

+0

然后你可以创建基于所有列值的查询。将每个单元格值传递给查询并删除该特定行。 –

0

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

0

私人无效button60_Click(对象发件人,EventArgs的){

 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); 

      } 
     } 
相关问题