从`DataBindingSource`在`DataGridView`中将数据添加到新添加的行中

问题描述:

Form4中我有一个名为DbTableDataGridViewDataGridView。 在Form3有一组字段(文本框),都绑定到DbTableBindingSource。当我运行应用程序时出现Form4。有一个按钮可以打开新表单(Form3),然后在此处输入有关要作为新行添加到数据库中的客户的详细信息(DataGridView)。我在Form4“添加”按钮,代码如下:从`DataBindingSource`在`DataGridView`中将数据添加到新添加的行中

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 


     Me.DbTableDataGridView.Refresh() 
     Me.DbTableBindingSource.AddNew() 

     Form3.ShowDialog() 

     Form3.ImiéTextBox.Text = "" 
     Form3.NazwiskoTextBox.Text = "" 
     Form3.Numer_TelefonuTextBox.Text = "" 
     Form3.Numer_RejestracyjnyTextBox.Text = "" 
     Form3.MarkaTextBox.Text = "" 
     Form3.ModelTextBox.Text = "" 
     Form3.Poj_SilnikaTextBox.Text = "" 
     Form3.RocznikTextBox.Text = "" 
     Form3.PaliwoTextBox.Text = "" 
     Form3.Data_PrzyjeciaDateTimePicker.Value = DateTime.Now 
     Form3.RichTextBox1.Text = "" 

    End Sub 

它确实增加了新行,选择它并清除在文本框中输入(被后装订成“DbTableBindingSource” 在这种形式。我填写的所有字段我按按钮保存:?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Try 



      Me.Validate() 

      Form4.DbTableBindingSource.EndEdit() 
      Me.DbTableTableAdapter.Update(CartronicDBDataSet.dbTable) 
      TableAdapterManager.UpdateAll(CartronicDBDataSet) 
      DbTableTableAdapter.Fill(Form4.CartronicDBDataSet.dbTable) 

      MsgBox("Saved") 


     Catch ex As Exception 
      MessageBox.Show("Blad zapisu. Sprobuj ponownie. W razie potrzeby zamknij, a nastepnie uruchom ponownie program Cartronic") 
     End Try 


    End Sub 

它去“拯救”的消息,但实际上是在增加了新的不填最近 任何想法

我已经完成了你的建议,但稍微简单一些。 分配的所有文本框,以每个单元格当前行中如下:

Form4.DbTableDataGridView.CurrentRow.Cells(5).Value = Me.NazwiskoTextBox.Text.ToString 
     Form4.DbTableDataGridView.CurrentRow.Cells(4).Value = Me.ImiéTextBox.Text.ToString 

它工作正常。 Cheers

没有联系我可以看到b在Form3和你的数据之间。

我建议将新创建的数据行传递给Form3的新实例。

  1. 获取关于你新添加的行
  2. 创建一个新的Form3(避免默认实例的形式,他们是丑陋和邪恶)。这意味着你不需要清除文本框,因为每次都有一个全新的表单。
  3. 传递数据行到您的表单,您将直接将其绑定到文本框

    Dim newRow = CType(Me.DbTableBindingSource.AddNew(), DataRow) 
    Using frmEditor As New Form3 
        frmEditor.DataSource = newRow 
        frmEditor.ShowDialog() 
    End Using 
    

在form3添加属性(或最好是构造函数)

Private mDataSource As DataRow 
Public Property DataSource As DataRow 
    Get 
     Return mDataSource 
    End Get 
    Set(value As DataRow) 
     mDataSource = value 
     Me.ImiéTextBox.DataBindings.Add("Text", mDataSource, "ImiéFieldName") 
     ' .... 
    End Set 
End Property 

如果使用这种方法,那么你可以摆脱所有的文本框清除代码。

+0

在Form3.Desiner.vb中,我更改了所有绑定了我的绑定源的文本框实例。最初它是Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding(“Text”,Me.DbTableBindingSource,“Dodano Przez”,True))。我将它们全部更改为Me.TextBox1.DataBindings.Add(新建System.Windows.Forms.Binding(“Text”,Form4.DbTableBindingSource,“Dodano Przez”,True))(从“我”到“Form4”) 。它确实运行正常,但我得到一个错误说:'WindowsApplication4.My.MyProject'没有名为'窗体'的属性。 –

+0

我确实使用文本属性 - > Data-> DataBindings-> Text来绑定Form3中的每个相关文本框,并选择了我的绑定源。但这似乎不起作用。它只有当我在代码中更改以绑定Form4时有效,但是出现此错误并且属性中的所有绑定都消失了。 –

+0

这里有几个问题。 1从不修改.designer文件中的任何内容。它会被覆盖。我自己不使用绑定源,所以我不知道它们的用法。所以我会说要么使用绑定源或使用我的方法。3永远不要以某种形式从另一种形式引用某些形式(例如,参考形式3中的form4.dbtablebinding)。 4从来没有使用默认的窗体实例,而是使用新的Form3等 – FloatingKiwi