拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 选择另一个控制元件时,datagridview中的新行默认值消失

选择另一个控制元件时,datagridview中的新行默认值消失

白鹭 - 2022-02-02 1992 0 0

这是我在这里的第一个问题,所以请对我仁慈。

目的:

我想要完成的是允许用户从 Windows 表单应用程序中的 DataGridView(系结到自定义类的物件串列)编辑行。此外,当 DataGridView 中生成新行时,我需要提供一些默认值,我正在使用 DataGridView 中的 DefaultValuesNeeded 事件处理程序实作这些默认值。

问题:编辑行时,用户必须能够导航到 DataGridView 之外(例如,导航到 TextBox 以提供额外信息),但是如果用户在编辑前离开新行,默认值将从行中消失这是我需要避免的。如果用户编辑新行的任何单元格,然后单击表单中的其他位置,则行中的所有值都保留在那里,这是正确的和所需的行为。

我创建了一个小项目来说明这一点。形式:

Imports System.ComponentModel

Public Class Form1
    Private Sub dgvAsientos_DefaultValuesNeeded(sender As Object, e As Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded

        e.Row.Cells("ID").Value = Me.DataGridView1.Rows.Count
        e.Row.Cells("Name").Value = "Test Name " & Me.DataGridView1.Rows.Count
        e.Row.Cells("Description").Value = "Description " & Me.TextBox1.Text & " " & Me.DataGridView1.Rows.Count
        Me.DataGridView1.BindingContext(Me.DataGridView1.DataSource, Me.DataGridView1.DataMember).EndCurrentEdit()

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim myList As New BindingList(Of ExampleClass)
        For n = 0 To 5
            Dim itemn As New ExampleClass
            itemn.ID = n
            itemn.Name = "Name_" & n
            itemn.Description = "Description_" & n
            itemn.OptionalField = "OptionalField_" & n

            myList.Add(itemn)
        Next
        Dim bs As New BindingSource()
        bs.DataSource = myList
        Me.DataGridView1.DataSource = bs
    End Sub
End Class

示例类:

Public Class ExampleClass
    Public Property ID As Integer
    Public Property Name As String
    Public Property Description As String
    Public Property OptionalField As String    
End Class

任何帮助将不胜感激。我发现关于 DefaultValuesNeeded BindingSources 用户关注其他控制元件时丢失的值的信息很少;其中一些让我在下面添加一行,但我没有发现这有什么不同。

(...).EndCurrentEdit()

我还发现了为系结源添加新事件添加处理程序的建议,该事件回传具有我需要的默认值的物件实体,同样没有区别。

  Private Sub myBindingSource_AddingNew(sender As Object, e As AddingNewEventArgs)    
        e.NewObject = CreateNewExample()
  End Sub

我希望问题和格式是正确的。提前致谢,MBD

uj5u.com热心网友回复:

当用户通过导航到最后一行然后在编辑单元格之前离开该行在 DataGridView 中添加新行时;在这种情况下,将取消添加行。这是因为某些内部逻辑会在行不脏时取消新行。

要改变这种行为,您可以处理RowValidatingRowLeave事件,然后通知当前单元格已脏,然后结束当前编辑

C# 示例

private void dgv1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == dgv1.NewRowIndex)
    {
        dgv1.NotifyCurrentCellDirty(true);
        dgv1.BindingContext[dgv1.DataSource, dgv1.DataMember].EndCurrentEdit();
        dgv1.NotifyCurrentCellDirty(false);
    }
}

VB.NET 示例

Private Sub dgv1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) _
Handles dgv1.RowLeave

    If e.RowIndex = dgv1.NewRowIndex Then
        dgv1.NotifyCurrentCellDirty(True)
        dgv1.BindingContext(dgv1.DataSource, dgv1.DataMember).EndCurrentEdit()
        dgv1.NotifyCurrentCellDirty(False)
    End If
End Sub
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *