2013-02-11 20 views
0

我有一個DataGridView,我希望CheckColumn中的所有行(我已命名'chk')爲他們對應的'ID'這是另一列(列1)DataGridView對應的列...與MySQL

我需要這些信息來朗姆酒mysql命令來刪除選中的用戶..

我不知道如何甚至開始請幫忙不恨:)

+0

*凹凸。我知道這不是一個很好的問題,但如果你能得到任何幫助,那就太棒了! – Kraxed 2013-02-11 18:25:54

回答

1

我不認爲你可以只用CheckColumn'chk'來做到這一點。相反,遍歷DataGridView的所有行,並確定CheckColumn的值是否爲真。

下面是一個例子,可以讓你開始。想象一下,你有一個按鈕點擊例程來刪除所有選中的行。在此例程中,當您遍歷DataGridView中的每一行時,將CheckColumn的值爲True的行存儲爲List。在我的例子中,我假設ID列是第一列,而CheckColumn是第二列。您可以調整我的示例中的數組索引以適合您的需要,或者按名稱而不是索引訪問列。對於被檢查的行,你可以調用一個執行你的SQL事務的函數。

當您完成在整個DataGridView去,經過您已存儲在您的列表行(即具有True託運的值)的列表,並從DataGridView刪除它們。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    //Use a list to store all the rows you need to delete 
    Dim RowsToDelete As New List(Of DataGridViewRow) 

    //Go through each row and find all the ones who's values are checked 
    For Each row As DataGridViewRow In DataGridView1.Rows 
     //Assume column at index 0 is the ID and column at index 1 is the CheckColumn 
     If row.Cells(1).Value = True Then 
      DeleteRow(row.Cells(0).Value) //This will call your SQL stored procedure and pass it the ID 
      RowsToDelete.Add(row) 
     End If 
    Next 

    //Delete all the rows from the DataGridView that were checked 
    For Each rowtodelete In RowsToDelete 
     DataGridView1.Rows.Remove(rowtodelete) 
    Next 
End Sub 

Private Sub DeleteRow(ByVal ID As Integer) 
    //Call an SQL function here with the ID Value 
    //To delete the record from the database 
End Sub 

你或許可以讓這個通過存儲ID在一個表,通過該表的價值,你的SQL程序執行得更好......我知道你可以在SQL Server 2008中做到這一點,但我不肯定關於MySQL。此外,如果SQL調用需要很長時間(在這種情況下看起來並不像這樣),您可能需要考慮在與用戶界面不同的線程上運行SQL命令。但是,如果沒有太大的進步,這應該足以讓你開始。

+0

我嘗試並使用mysql命令,它表示ID可能在運行時導致爲null。 – Kraxed 2013-02-11 23:16:58

+0

@Kraxed只是一個警告?如果是這樣,你可以在調用你的sql函數之前檢查id是否爲空。 – Mash 2013-02-12 01:25:49