2015-06-20 55 views
1

我必須使用textbox過濾datagridview。下面的代碼用於填充db類的gridview.getdata函數返回數據表。在Windows應用程序中的DataGridview中搜索VB.NET

我沒有使用gridview的datasource屬性,而是使用循環提交gridview。

我可以使用datasource屬性和dataview進行搜索,但我沒有直接從datasource屬性填充datagridview。

Sub griddesgn() 
    DataGridView1.Columns.Clear() 
    DataGridView1.Rows.Clear() 
    DataGridView1.Columns.Add("crime", "crime") 
    DataGridView1.Columns.Add("actname", "actname") 
    DataGridView1.Columns.Add("section", "section") 
    DataGridView1.Columns.Add("description", "description") 
End Sub 

Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    griddesgn() 
    Dim DBOBJ As New db 
    Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women") 

    If DTT.Rows.Count > 0 Then 
     For i As Integer = 0 To DTT.Rows.Count - 1 
      DataGridView1.Rows.Add() 
      DataGridView1.Rows(i).Cells("crime").Value = DTT.Rows(i).Item("crime") & "" 
      DataGridView1.Rows(i).Cells("actname").Value = DTT.Rows(i).Item("actname") & "" 
      DataGridView1.Rows(i).Cells("section").Value = DTT.Rows(i).Item("section") & "" 
      DataGridView1.Rows(i).Cells("description").Value = DTT.Rows(i).Item("description") & "" 
     Next 
    End If 

End Sub 
+0

對不起,這對我來說還不清楚。你在標題中提到搜索,但你(隱含地)討論在你的問題中使用'ItemsSource'。 –

+0

我編程地填充數據在gridview中使用for循環從datatable dt由getdata函數返回。現在數據應該過濾,只要我開始在顯示的文本框中鍵入。它說... – user3449614

+2

綁定DataTable到DataGridView與BindingSource 。然後,使用BindingSource的Filter屬性。 – Graffito

回答

0

使用WHERE語句在SQL查詢

"SELECT crime,actname,section,description from natureofcomplaint_women WHERE crime = " & txtSearch.text 

(如果你想在犯罪搜索) 更改您的需求。 只是重複數據網格填充你上面使用,但是有一個altereted SQL查詢每次輸入/按搜索按鈕

0

我仍然認爲這是最好的結合...

Sub griddesgn() 
DataGridView1.Columns.Clear() 
DataGridView1.Rows.Clear() 
DataGridView1.Columns.Add("crime", "crime") 
DataGridView1.Columns.Add("actname", "actname") 
DataGridView1.Columns.Add("section", "section") 
DataGridView1.Columns.Add("description", "description") 
End Sub 

Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    griddesgn() 
    Dim DBOBJ As New db 
    Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women") 

    Dim source1 as New BindingSource() 
    source1.DataSource = DTT 
    source1.Filter = "crime = '" & textboxtCrime.text & "'" 
    DataGrideView1.DataSource = source1 

End Sub 
+0

它工作正常,但我必須應用一些格式化從datatable.thats我用於迴路填充gridview返回的數據。 – user3449614

+0

好吧,如果您必須使用for循環,您可以在If塊中包含它的複製部分,該塊只包含符合所有條件的行:我將在下面的示例中以幾分鐘的形式發佈另一個答案。 – DiscipleMichael

+0

請張貼您的答案。等待4我的回覆 – user3449614

0
Sub griddesgn() 
    DataGridView1.Columns.Clear() 
    DataGridView1.Rows.Clear() 
    DataGridView1.Columns.Add("crime", "crime") 
    DataGridView1.Columns.Add("actname", "actname") 
    DataGridView1.Columns.Add("section", "section") 
    DataGridView1.Columns.Add("description", "description") 
End Sub 

Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    griddesgn() 
    Dim DBOBJ As New db 
    Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women") 

    If DTT.Rows.Count > 0 Then 
     For i As Integer = 0 To DTT.Rows.Count - 1 
     If (DTT.Rows(i).Item("Crime").Contains(txtCrimeFilter.Text) AND & _ 
       (DTT.Rows(i).Item("actname").Contains(txtActnameFilter.Text) AND & _ 
       (DTT.Rows(i).Item("section").Contains(txtSectionFilter.Text)  AND & _ 
       (DTT.Rows(i).Item("description").Contains(txtDescriptionFilter.Text) 
       DataGridView1.Rows.Add() 
       DataGridView1.Rows(i).Cells("crime").Value = DTT.Rows(i).Item("crime") & "" 
       DataGridView1.Rows(i).Cells("actname").Value = DTT.Rows(i).Item("actname") & "" 
       DataGridView1.Rows(i).Cells("section").Value = DTT.Rows(i).Item("section") & "" 
       DataGridView1.Rows(i).Cells("description").Value = DTT.Rows(i).Item("description") & "" 
     End If 
    Next 
End If 

結束Sub

+0

這假定你有一個過濾器文本框的每個4列......如果沒有,然後刪除它們。另外,上面的過濾器是區分大小寫的......如果你不想這樣做,那麼就把它放到行上和textbox.text上。 – DiscipleMichael

+0

究竟是什麼部分的要求不符合?它以編程方式填充datagridview並應用過濾器。 – DiscipleMichael

相關問題