根据文本框值突出显示Datagridview中的行

时间:2016-08-31 13:50:24

标签: vb.net datagridview

晚安。

我在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未来的帮助

1 个答案:

答案 0 :(得分:0)

使用DataGridView.CurrentRow属性。但请注意,CurrentRowReadOnly,您必须使用CurrentCell

在刷新数据存储Dim oldIndex = DataGridView.CurrentRow.Index之前和刷新集DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)

之后

修改

如何测试代码

使用Button1DataGridView1创建一个包含两列的表单并粘贴以下代码:

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)