在输入时更改 datagridview 中的编辑单元格 (vb.net)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19406308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change editted cell in datagridview on enter (vb.net)
提问by Osprey
I have a datagridview table that is populated by using a datatable as a datasource. The datagridview has been set to edittable and I can change the values of cells but of course, the changes do not reflect in the original database table since the grid view is not directly bound. Is it possible to have the datagridview in such a way that when I press enter (when editting a cell), the focus shifts to the cell on the right (rather then selecting the row below)? This would need to keep on going until I reach the right most column, in which case the following edit cell would be the first cell in the following row.
我有一个 datagridview 表,它是通过使用数据表作为数据源来填充的。datagridview 已设置为edittable,我可以更改单元格的值,但当然,更改不会反映在原始数据库表中,因为网格视图没有直接绑定。是否可以以这样的方式使用 datagridview,当我按 Enter 键(编辑单元格时)时,焦点会转移到右侧的单元格(而不是选择下面的行)?这将需要继续进行,直到我到达最右边的列,在这种情况下,以下编辑单元格将是下一行中的第一个单元格。
Thanks in advance!
提前致谢!
回答by Andrea
Try this:
尝试这个:
define a flag
flag_editedthat will be raised when an edit occurs (CellEndEditevent)define a function
changeSelectionToNextCellthat will undo default behavior's selection change (SelectionChangedevent)
定义一个
flag_edited在编辑发生时将被引发的标志(CellEndEdit事件)定义一个函数
changeSelectionToNextCell来撤销默认行为的选择更改(SelectionChanged事件)
Here is a sample:
这是一个示例:
Private flag_edited As Boolean
Private Sub DataGridView1_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
flag_edited = True
End Sub
Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
changeSelectionToNextCell()
End Sub
Private Sub changeSelectionToNextCell()
If flag_edited Then
flag_edited = False
If DataGridView1.CurrentCell IsNot Nothing Then
Dim row_index As Integer = DataGridView1.CurrentCell.RowIndex - 1
Dim col_index As Integer = DataGridView1.CurrentCell.ColumnIndex + 1
If col_index = DataGridView1.ColumnCount Then
col_index = 0
row_index += 1
End If
DataGridView1.CurrentCell = DataGridView1.Rows(row_index).Cells(col_index)
End If
End If
End Sub

