在datagridview更新后继续关注行

时间:2015-07-17 06:24:52

标签: vb.net datagridview focus

我正在创建一个VB Windows应用程序。应用程序的要点很简单DataGridView,我从SQL Server数据库中获取View

DataGridView每秒刷新一次,这样我就能在GridView中看到新的数据收入。

问题是在刷新后继续关注行。我需要解决方案,在我点击一行之后,或者一个单元格,即使在刷新之后它也会让我保持它。

这是我的代码:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Refresh every 1 sec
        Dim timer As New Timer()
        timer.Interval = 1000
        AddHandler timer.Tick, AddressOf timer_Tick
        timer.Start()
        'TODO: This line of code loads data into the 'XYZDataSet.view1' table. You can move, or remove it, as needed.
        Me.View1TableAdapter.Fill(Me.XYZDataSet.view1)
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    End Sub

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells("DayTillDelivery").Value <= 30 Then
                Me.DataGridView1.Rows(i).Cells("DayTillDelivery").Style.ForeColor = Color.Red
            End If
        Next
    End Sub

    Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        'Calling refresh after 1 second and updating the data
        Me.DataGridView1.Refresh()
        Me.View1TableAdapter.Fill(Me.XYZDataSet.view1)
    End Sub
End Class

3 个答案:

答案 0 :(得分:2)

我在过去通过在执行刷新之前将所选单元格的索引存储在变量中来解决了类似的问题,因此我可以通过在更新后调用DataGridView.Rows(selRow).Cells(selCol).Selected = True来恢复选择。 / p>

编辑 - 示例代码:

  

以后的读者:
请看一下编辑#2,其中我描述了一种更好的方法来重新选择之前选择的单元格!

示例代码:

'   Variables for remembering the indexes of the selected cell
Dim selRow As Integer
Dim selCol As Integer

'   Check if there is a selected cell to prevent NullPointerException
If DataGridView1.SelectedCells().Count > 0 Then
    selRow = DataGridView1.CurrentCell.RowIndex
    selCol = DataGridView1.CurrentCell.ColumnIndex
End If

'   Dummy "update"
'   don't forget to clear any existing rows before adding the new bunch (only if you always reloading all rows)!
DataGridView1.Rows.Clear()
For index = 1 To 20
    DataGridView1.Rows.Add()
Next

'   Check if there are "enough" rows after the update, 
'   to prevent setting the selection to an rowindex greater than the Rows.Count - 1 which would 
'   cause an IndexOutOfBoundsException
If (DataGridView1.Rows.Count - 1) > selRow Then
    '   Clear selection and then reselect the cell that was selected before by index
    DataGridView1.ClearSelection()
    '   For the next line of code, there is a better solution in Edit #2!
    DataGridView1.Rows(selRow).Cells(selCol).Selected = True       
End If

请注意:

  • 此过程要求您以与更新之前添加的行完全相同的顺序添加行,因为只有所选行的.Index存储在变量中。如果您以不同的顺序读取行,则刷新后将选择不是同一行但同一位置的行
  • 您应该添加检查是否存在所选行(以防止NullPointerException)以及之后{/ 1}} 中是否有“足够”行,以防止DataGridView
  • 这仅适用于IndexOutOfBoundsException实际选择行的内容,例如DataGridView1.SelectionMode
  • 在通过更新添加新行之前,不要忘记清除任何现有行(仅当您总是重新加载所有行时)。

编辑2 - RowHeader三角形和意外的MultiSelect

如下面的评论中所述,如果用户按住鼠标按钮超过刷新周期,则会出现导致意外MultiSelect的奇怪行为。此外,RowHeader三角形未设置为正确的行。

经过一番研究后,我找到了解决这种问题的方法。不是将给定单元格的FullRowSelect - 属性设置为.Selected,而是将True - .CurrentCell - 属性设置为您要选择的单元格。

在代码中,这意味着更改

DataGridView

<强> DataGridView1.Rows(selRow).Cells(selCol).Selected = True

然后你去。 : - )

答案 1 :(得分:2)

在填充之前,存储CurrentRow值和currenCell列:

Dim currentColumnIndex As Integer = 0 ;
Dim currentValues As List(Of Object) = If(DataGridView1.CurrentRow Is Nothing, Nothing, New List(Of Object)())
If currentValues IsNot Nothing Then
    For i As Integer = 0 To DataGridView1.Columns.Count - 1
        currentValues.Add(DataGridView1.CurrentRow.Cells(i).Value)
    Next
currentColumnIndex = DataGridView1.CurrentCell.ColumnIndex;
End If

填写后,搜索与存储值对应的行:

Dim i As Integer = 0
While i < DataGridView1.Rows.Count AndAlso currentValues IsNot Nothing
    Dim areIdentical As Boolean = True
    Dim j As Integer = 0
    While j < DataGridView1.Columns.Count AndAlso areIdentical
        areIdentical = DataGridView1.Rows(i).Cells(j).Value = currentValues(j)
        j += 1
    End While
    If areIdentical Then
        DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(currentColumnIndex)
        currentValues = Nothing
    End If
    i += 1
End While

注意:&#34; For / While&#34;循环编码可能不是最佳的,因为它是从C#到vb.net的自动转换产生的。

答案 2 :(得分:0)

C#修复代码,下一个重新加载模式

if (dataGridView7.SelectedCells.Count > 0)
{
    //MessageBox.Show(selcell + "------"+dataGridView7.CurrentCell.ColumnIndex.ToString());
    if (selcell > 0 && dataGridView7.CurrentCell.ColumnIndex==0) {  }else
    {
        selrow = dataGridView7.CurrentCell.RowIndex;
        selcell = dataGridView7.CurrentCell.ColumnIndex;
    }

}

loaddataJobsall();
dataGridView7.ClearSelection();
dataGridView7.Rows[selrow].Cells[selcell].Selected = true;