在不改变焦点的情况下将 datagridview 保存到 CSV
Save datagridview to CSV without changing focus
我正在使用以下代码将我的 DataGridView table 保存到文本文件:
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where Not row.IsNewRow _
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
Using sw As New IO.StreamWriter("Z:\SchData.txt")
For Each r In rows
sw.WriteLine(String.Join(";", r))
Next
End Using
并更新以下 table:
While 1 = 1
Try
DataGridView1.Rows.Clear()
Using stream As System.IO.FileStream = System.IO.File.OpenRead("Z:\SchData.txt")
Using reader As New System.IO.StreamReader(stream)
Dim line As String = reader.ReadLine()
While (line IsNot Nothing)
Dim columns = line.Split(";")
line = reader.ReadLine()
Dim index = Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(index).SetValues(columns)
End While
End Using
End Using
Button107.Enabled = True
Button88.Enabled = True
updatingSch = False
Return
Catch ex As Exception
Threading.Thread.Sleep(50)
End Try
End While
当我更改 table 中的值时,将调用这两个函数。问题是,它不是转到下一个单元格(当我切换、向下输入或单击另一个单元格时),而是将焦点短暂地设置到正确的单元格,但随后快速切换到左上角的单元格(第 0 行,第 1 列) 0).我怎样才能阻止它改变焦点?
这是编辑后保持在同一行的方法。
Private Sub dgv_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit
Dim index As Integer = dgv.FirstDisplayedScrollingRowIndex
'do your file work then reset the row index
dgv.FirstDisplayedScrollingRowIndex = index
End Sub
我正在使用以下代码将我的 DataGridView table 保存到文本文件:
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where Not row.IsNewRow _
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
Using sw As New IO.StreamWriter("Z:\SchData.txt")
For Each r In rows
sw.WriteLine(String.Join(";", r))
Next
End Using
并更新以下 table:
While 1 = 1
Try
DataGridView1.Rows.Clear()
Using stream As System.IO.FileStream = System.IO.File.OpenRead("Z:\SchData.txt")
Using reader As New System.IO.StreamReader(stream)
Dim line As String = reader.ReadLine()
While (line IsNot Nothing)
Dim columns = line.Split(";")
line = reader.ReadLine()
Dim index = Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(index).SetValues(columns)
End While
End Using
End Using
Button107.Enabled = True
Button88.Enabled = True
updatingSch = False
Return
Catch ex As Exception
Threading.Thread.Sleep(50)
End Try
End While
当我更改 table 中的值时,将调用这两个函数。问题是,它不是转到下一个单元格(当我切换、向下输入或单击另一个单元格时),而是将焦点短暂地设置到正确的单元格,但随后快速切换到左上角的单元格(第 0 行,第 1 列) 0).我怎样才能阻止它改变焦点?
这是编辑后保持在同一行的方法。
Private Sub dgv_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit
Dim index As Integer = dgv.FirstDisplayedScrollingRowIndex
'do your file work then reset the row index
dgv.FirstDisplayedScrollingRowIndex = index
End Sub