簡體   English   中英

根據文本框值在Datagridview中突出顯示一行

[英]Highlight a row in Datagridview based on textbox value

晚上好。

我在VB.Net中有一個程序,可在您編輯一些數據后刷新datagridview。我的問題是在這里填充1000多個記錄。

假設我正在編輯第999行,然后單擊“更新”,數據將刷新,導致datagridview返回頂部(藍色熒光筆)

我的目標是更新后如何將其保持在當前位置?

我的解決方案是突出顯示Textbox1 = value的數據

這樣可能嗎?

'SAMPLE CODE
Datagridview1.Column(0).value.BlueHighLighter = Textbox1.text

請查看有關如何刷新DGV的代碼

  Dim con11 As MySqlConnection = New MySqlConnection("server=192.168.2.87;userid=root;password=admin1950;database=inventory")
        Dim sql1 As MySqlCommand = New MySqlCommand("select PONo,ItemCode,Description,QtyPack,PackUoM,QtyStan,StanUoM,UnitPrice,Total,Remarks,ExpiryDate from Receiving where RINo = '" & Add_Receiving_Items.TextBox1.Text & "';", con1)
        Dim ds1 As DataSet = New DataSet
        Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
        con1.Open()
        adapter1.SelectCommand = sql1
        adapter1.Fill(ds1, "MyTable")
        Add_Receiving_Items.DataGridView1.DataSource = ds1.Tables(0)
        con1.close()
        With Add_Receiving_Items.DataGridView1()
            .RowHeadersVisible = False
            .Columns(0).HeaderCell.Value = "PO No"
            .Columns(1).HeaderCell.Value = "Item Code"
            .Columns(2).HeaderCell.Value = "Description"
            .Columns(3).HeaderCell.Value = "Quantity/Pack"
            .Columns(4).HeaderCell.Value = "Packaging UoM"
            .Columns(5).HeaderCell.Value = "Quantity/Pc"
            .Columns(6).HeaderCell.Value = "Standard UoM"
            .Columns(7).HeaderCell.Value = "Unit Price"
            .Columns(8).HeaderCell.Value = "Total"
            .Columns(9).HeaderCell.Value = "Remarks"
            .Columns(10).HeaderCell.Value = "Expiry Date"
        End With

        Add_Receiving_Items.DataGridView1.Columns.Item(0).Width = 80
        Add_Receiving_Items.DataGridView1.Columns.Item(1).Width = 80
        Add_Receiving_Items.DataGridView1.Columns.Item(2).Width = 120
        Add_Receiving_Items.DataGridView1.Columns.Item(3).Width = 86
        Add_Receiving_Items.DataGridView1.Columns.Item(4).Width = 68
        Add_Receiving_Items.DataGridView1.Columns.Item(5).Width = 75
        Add_Receiving_Items.DataGridView1.Columns.Item(6).Width = 68
        Add_Receiving_Items.DataGridView1.Columns.Item(7).Width = 70
        Add_Receiving_Items.DataGridView1.Columns.Item(8).Width = 80
        Add_Receiving_Items.DataGridView1.Columns.Item(9).Width = 105
        Add_Receiving_Items.DataGridView1.Columns.Item(10).Width = 63
        Add_Receiving_Items.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        Add_Receiving_Items.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        With Add_Receiving_Items.DataGridView1
            .RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
            .AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender
        End With

TYSM為將來提供幫助

使用DataGridView.CurrentRow屬性。 但是請注意, CurrentRowReadOnly ,必須使用CurrentCell

在刷新數據存儲之前, Dim oldIndex = DataGridView.CurrentRow.Index以及刷新后設置DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)

編輯:

如何測試代碼

創建一個帶有Button1和一個帶有兩列的DataGridView1的表單,並粘貼以下代碼:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 1 To 5
            DataGridView1.Rows.Add("foo" & i, "bar" & i)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim currentIndex = DataGridView1.CurrentRow.Index
        currentIndex += 1
        If currentIndex >= DataGridView1.Rows.Count Then currentIndex = 0

        DataGridView1.CurrentCell = DataGridView1.Rows(currentIndex).Cells(0)
    End Sub
End Class

編輯:

Dim oldIndex = DataGridView.CurrentRow.Index
'Put your code here on how you refresh your data
DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM