使用文本框查找并移动到datagridview中的特定单元格

时间:2016-09-30 05:44:12

标签: vb.net datagridview

我在VB.Net中有一个如下所示的程序:

enter image description here

它有很多数据,我有一个像这样的搜索模块:

enter image description here

以下是关于我如何填充的代码:

Route::get('/', ['middleware' => 'nameMiddleware'] ...);

说真的,我试过这个,但它没有用。

 Dim con1 As MySqlConnection = New MySqlConnection("server=192.168.2.250;userid=root;password=admin1950;database=inventory")
    Dim sql1 As MySqlCommand = New MySqlCommand("Select Location,Category,ItemCode,Description,UOM,BegInv,Receiving,AvailableStocks,StockIssuance,EndingStocks,StoretoWarehouse,WarehouseToSup,StockAdjust,TheoCount,PhysicalCount,Variance,Remarks from variance", con1)
    Dim ds1 As DataSet = New DataSet
    Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
    con1.Open()
    adapter1.SelectCommand = sql1
    adapter1.Fill(ds1, "MyTable")
    DataGridView1.DataSource = ds1.Tables(0)
    con1.Close()
    With DataGridView1
        .RowHeadersVisible = False
        .Columns(0).HeaderCell.Value = "Location"
        .Columns(1).HeaderCell.Value = "Category"
        .Columns(2).HeaderCell.Value = "Item Code"
        .Columns(3).HeaderCell.Value = "Description"
        .Columns(4).HeaderCell.Value = "UOM"
        .Columns(5).HeaderCell.Value = "Beg. Inventory"
        .Columns(6).HeaderCell.Value = "Receiving"
        .Columns(7).HeaderCell.Value = "Available Stocks"
        .Columns(8).HeaderCell.Value = "Stock Issuance"
        .Columns(9).HeaderCell.Value = "Ending Stocks"
        .Columns(10).HeaderCell.Value = "Store to Warehouse"
        .Columns(11).HeaderCell.Value = "Warehouse to Supplier"
        .Columns(12).HeaderCell.Value = "Stock Adjustment"
        .Columns(13).HeaderCell.Value = "Theoretical Qty."
        .Columns(14).HeaderCell.Value = "Physical Count"
        .Columns(15).HeaderCell.Value = "Variance"
        .Columns(16).HeaderCell.Value = "Remarks"
        .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        .Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        .Columns.Item(0).Width = 125
        .Columns.Item(1).Width = 102
        .Columns.Item(2).Width = 77
        .Columns.Item(3).Width = 236
        .Columns.Item(4).Width = 53
        .Columns.Item(5).Width = 73
        .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .Columns(5).Frozen = True
    End With

1 个答案:

答案 0 :(得分:0)

我自己诚实地称之为长方法,因为我知道有更好的答案可以整合LINQ(不是精通LINQ

首先,在搜索表单中声明此变量:

Public dt as New Datatable

其次,在Form1(您的DataGridView1所在的位置)声明此内容, 这将在打开您的搜索表单之前暂时保留记录:

Dim tempdt as New Datatable

然后在设置DataGridView1数据源之前包含此代码。 紧接着:

...
DataGridView1.DataSource = ds1.Tables(0)
tempdt  = New Datatable
tempdt  = ds1.Tables(0)
...

然后,像这样调用您的搜索表单(如果您有现有代码,请将其替换为此):

Dim srchForm as New YourSearchFormName
srchForm.dt = tempdt
srchForm.Show()

然后在搜索表单的查找按钮中包含此代码:

    Dim drow() As DataRow
    drow = dt.Select("ItemCode LIKE '%" & txtSearch.Text & "%' OR Description LIKE '%" & txtSearch.Text & "%'")
    If drow.Count > 0 Then
        'THIS WILL LOOP THE GRID BASED ON THE RECORDS FOUND BY TXTSEARCH.TEXT
        For Each row As DataRow In drow
            Dim ItemCodeStr As String = row.Item("ItemCode").ToString
            For Each dgrow As DataGridViewRow In Form1.DataGridView1.Rows
                If ItemCodeStr = dgrow.Cells(2).Value.ToString Then
                    dgrow.DefaultCellStyle.BackColor = Color.Pink
                End If
            Next
        Next
    Else
        MsgBox("There are no matches found.", MsgBoxStyle.Information, "Result")
    End If

在此代码中,我使用 ItemCode Description 作为条件。当符合搜索条件时,它会将背景颜色更改为 Pink 。我用 Pink 因为它是我最喜欢的颜色。