Datagridview 在第一个之后插入新行

Jiale Xue - MSFT 37,136 信誉分 Microsoft 供应商
2024-03-22T08:08:18.6833333+00:00

请我需要帮助,因为当用户按回车键时,我正在使用 datagridview,当它到达最后一个单元格时,它会移动到下一个单元格,而不是新到新行

用户在最后一个单元格或行上输入键后,将其移动到新行,以下代码是插入新行,但它插入行作为第一行,我希望新插入的行将添加为用户填充的第二行,将插入第三行新行,依此类推

我的代码如下。

Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    If e.KeyCode = Keys.Enter Then


        Dim c As DataGridViewCell = DataGridView1.CurrentCell
        If c.ColumnIndex = DataGridView1.ColumnCount - 1 And c.RowIndex < DataGridView1.RowCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex + 1).Cells(0)

        ElseIf c.ColumnIndex < DataGridView1.ColumnCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex).Cells(c.ColumnIndex + 1)

        ElseIf c.ColumnIndex = DataGridView1.ColumnCount - 1 Then


            DataGridView1.Rows.Add(c.RowIndex + 1)
        End If
        e.Handled = True
    End If
End Sub

Private Sub ec_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ec.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then

        DataGridView1.EndEdit()
        Dim c As DataGridViewCell = DataGridView1.CurrentCell
        If c.ColumnIndex = DataGridView1.ColumnCount - 1 And c.RowIndex < DataGridView1.RowCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex + 1).Cells(0)

        ElseIf c.ColumnIndex < DataGridView1.ColumnCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex).Cells(c.ColumnIndex + 1)

        End If
    End If
End Sub

Note:此问题总结整理于:Datagridview insert new Row after First

C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
140 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 44,186 信誉分 Microsoft 供应商
    2024-03-22T09:34:02.21+00:00

    尝试添加此代码,当 DataGridView 处于 EditMode 模式时,它可以按 Enter 键来获取满足您要求的响应。

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean  
            If keyData = Keys.Enter Then  
      
                If DataGridView1.CurrentCell.ColumnIndex < DataGridView1.ColumnCount - 1 Then  
                    DataGridView1.CurrentCell =   
                             DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(DataGridView1.CurrentCell.ColumnIndex + 1)  
                    Return True  
                Else  
      
                    Try  
                        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex + 1).Cells(0)  
                        Return True  
                    Catch ex As ArgumentOutOfRangeException  
                        Return True  
                    End Try  
                End If  
            End If  
            Return MyBase.ProcessCmdKey(msg, keyData)  
        End Function  
    

    更新: 如果要在 datagridview 中间添加一个空白行以添加数据。可以使用 DataTable.NewRow 方法

    获取 CellClick 事件中的当前行数,并将空白行添加到下一行。

    同时,为了防止添加过多的空白行,我还对当前数据表添加了一个验证。如果已有空行,则无法继续添加新的空行。

        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick  
            Dim i As Integer = dt.AsEnumerable().Where(Function(row) IsDBNull(row("ID")) OrElse IsDBNull(row("Name")) OrElse IsDBNull(row("Score"))).Count()  
            If i = 0 Then  
                Dim newRow As DataRow = dt.NewRow()  
                dt.Rows.InsertAt(newRow, e.RowIndex + 1)  
            End If  
        End Sub  
    

    事实上,我觉得如果你在datagridview中添加一个新的按钮列,那么在单击按钮时添加新行可能会更容易。


    如果回复有帮助,请点击“接受答案”并点赞。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助