如何使用按钮按ID搜索DataGridView?

时间:2016-12-06 18:23:36

标签: c# .net vb.net datagridview

我有DataGridView BookNumberTitleAuthorNumPricequantityInStock。我想在用户按下按钮时按书号搜索行。这就是我所拥有的,但它不起作用。

如何只显示用户输入的图书编号的行?现在,当我按下输入时,没有出现任何行。任何帮助将不胜感激。

    Dim str As String
    str = InputBox("Enter an Book Number:", "Book Selection", "")
    TblBooksTableAdapter.Fill(BooksDataSet.tblBooks)

    Dim nums = (From n In BooksDataSet.tblBooks
         Where n.BookNumber = str
         Select n.BookNumber, n.Title, n.AuthorNum, n.Price, n.QuantityInStock).ToList

    DataGridView1.DataSource = BooksDataSet

2 个答案:

答案 0 :(得分:0)

你需要一个'DataView'恕我直言,以最简单的方式做到这一点。 DataGridViews也比DataOable更好地使用DataTable,因此我通常会在设计器上设置一个。在这种情况下,我将一个名为'ds'的数据集拖放到我的设计图面上,而不是在'Tables'集合上创建'tProducts'。代码示例如下:

Public Class DynamicFilterOnTwoTextItems

  Public Class ProductSku
    Public Property ProductId As Integer
    Public Property SkuID As Integer
    Public Property Description As String
  End Class

  Private _products As List(Of ProductSku) = New List(Of ProductSku)
  Private _initialLoadDone As Boolean = False

  Private Sub DynamicComboBoxDoubleFill_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dgv.AutoGenerateColumns = False

    Dim products = New List(Of ProductSku)({
                                     New ProductSku With {.ProductId = 1, .SkuID = 1, .Description = "Offline"},
                                     New ProductSku With {.ProductId = 1, .SkuID = 2, .Description = "Offline2"},
                                     New ProductSku With {.ProductId = 2, .SkuID = 1, .Description = "Online"},
                                     New ProductSku With {.ProductId = 2, .SkuID = 2, .Description = "Online2"}
                                     })

    Dim s = ""
    For Each o In _products
      Dim row As DataRow = ds.Tables("tProducts").NewRow
      row("ProductId") = o.ProductId
      row("SkuID") = o.SkuID
      row("Desc") = o.Description
      ds.Tables("tProducts").Rows.Add(row)
    Next

    dgv.AutoGenerateColumns = False
    dgv.DataSource = ds.Tables("tProducts")

    _initialLoadDone = True
  End Sub

  Private Sub bFilter_Click(sender As Object, e As EventArgs) Handles bFilter.Click

    Dim predicate = String.Empty

    If String.IsNullOrEmpty(tsku.Text) Then
      predicate = $"ProductID = '{CInt(tPrd.Text)}'"
    Else
      predicate = $"ProductID = '{CInt(tPrd.Text)}' and SkuID = '{CInt(tsku.Text)}'"
    End If
    Dim dvProducts = New DataView(ds.Tables("tProducts"), predicate, "ProductID ASC", DataViewRowState.CurrentRows)

    dgv.DataSource = dvProducts
  End Sub
End Class

答案 1 :(得分:0)

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles 

SearchTextBox.TextChanged

MainListView.Clear()
    DT = ExecuteQuery("SELECT * FROM Prac4 WHERE LastName LIKE '%" & SearchTextBox.Text & "%' OR FirstName Like '%" & SearchTextBox.Text & "%'")
    With MainListView
        .View = View.Details
        .GridLines = True
        .FullRowSelect = True
    End With
    MainListView.Columns.Add("EmpID", 100, HorizontalAlignment.Left)
    MainListView.Columns.Add("FirstName", 100, HorizontalAlignment.Left)
    MainListView.Columns.Add("LastName", 100, HorizontalAlignment.Left)
    MainListView.Columns.Add("MiddleName", 100, HorizontalAlignment.Left)
    MainListView.Columns.Add("DateHire", 100, HorizontalAlignment.Left)
    MainListView.Columns.Add("EmpType", 100, HorizontalAlignment.Left)
    MainListView.Columns.Add("Rate", 100, HorizontalAlignment.Left)
    For i As Integer = 0 To DT.Rows.Count - 1
        Dim xItem As New ListViewItem(DT.Rows(i)("EmpID").ToString)
        xItem.SubItems.Add(DT.Rows(i)("FirstName").ToString)
        xItem.SubItems.Add(DT.Rows(i)("LastName").ToString)
        xItem.SubItems.Add(DT.Rows(i)("MiddleName").ToString)
        xItem.SubItems.Add(DT.Rows(i)("DateHire").ToString)
        xItem.SubItems.Add(DT.Rows(i)("EmpType").ToString)
        xItem.SubItems.Add(DT.Rows(i)("Rate").ToString)
        MainListView.Items.Add(xItem)
    Next
End Sub

尝试改变其中的内容。