2012-05-29 120 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); 

      } 
     } 
相關問題