使用来自vb.net中另一个表单的datagridview自动填充

时间:2017-09-17 08:55:35

标签: vb.net datagridview

嘿伙计们我想使用来自另一种形式的datagridview进行自动填充,但问题是我不能让值回到以前的形式它只是继续创建一个新的form.it工作doe数据正在转移但是在另一个新表格请帮忙

    Dim form As New FormLibrarianMain

    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)

        form.Labelid.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
        form.Labelfirstname.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
        form.Labelmiddlename.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
        form.Labellastname.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
        form.Labelgender.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString()
        form.Labeladdress.Text = DataGridView1.CurrentRow.Cells(5).Value.ToString()
        form.Labelcity.Text = DataGridView1.CurrentRow.Cells(6).Value.ToString()
        form.Labelpostcode.Text = DataGridView1.CurrentRow.Cells(7).Value.ToString()
        form.Labelemail.Text = DataGridView1.CurrentRow.Cells(8).Value.ToString()
        form.Labeldob.Text = DataGridView1.CurrentRow.Cells(9).Value.ToString()

        form.Labeltype.Text = DataGridView1.CurrentRow.Cells(10).Value.ToString()


        form.ShowDialog()
        Me.Hide()







    End If

End Sub

3 个答案:

答案 0 :(得分:0)

如果您想将数据从一个Form传递到其他Form。您可以创建public变量来存储values

Class Form2
   public values as New ArrayList

   Private Sub FormLoad(..)
     if values.Count >= 0 then
         TextBox1.Text = values(1)
         TextBox2.Text = values(2)
     End If
   End Sub
End Class

来自 Form1:

If e.RowIndex >= 0 Then
    Dim row As DataGridViewRow
    row = Me.DataGridView1.Rows(e.RowIndex)

    With Form2
       .values = New ArrayList
       .values(0) = DataGridView1.CurrentRow.Cells(0).Value.ToString()
       .values(1) = DataGridView1.CurrentRow.Cells(1).Value.ToString()
       .values(2) = DataGridView1.CurrentRow.Cells(2).Value.ToString()
       .values(3) = DataGridView1.CurrentRow.Cells(3).Value.ToString()
       ' etc'
       .ShowDialog()
    End With
    Me.Hide()
End If

答案 1 :(得分:0)

你需要在你正在调用新形式的形式的全局变量中输出你的表单对象。将这样的内容添加到全局变量

  private LibrarianFormLoaded as boolean = false

然后在您的子

sub IJustClickedSomething (sender As Object, e As EventArgs)

if LibrarianFormLoaded = false then  form = New FormLibrarianMain : LibrarianFormLoaded=true

    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)

        form.Labelid.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
        form.Labelfirstname.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
        form.Labelmiddlename.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
        form.Labellastname.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
        form.Labelgender.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString()
        form.Labeladdress.Text = DataGridView1.CurrentRow.Cells(5).Value.ToString()
        form.Labelcity.Text = DataGridView1.CurrentRow.Cells(6).Value.ToString()
        form.Labelpostcode.Text = DataGridView1.CurrentRow.Cells(7).Value.ToString()
        form.Labelemail.Text = DataGridView1.CurrentRow.Cells(8).Value.ToString()
        form.Labeldob.Text = DataGridView1.CurrentRow.Cells(9).Value.ToString()

        form.Labeltype.Text = DataGridView1.CurrentRow.Cells(10).Value.ToString()


        form.ShowDialog()
        Me.Hide()







    End If

然后,如果表单确实已卸载,则添加此代码以使表单重新加载

LibrarianFormLoaded = false

如果您隐藏了librarianform而不是处理它,那么您可以读取控件的所有值,因为表单不可见。

答案 2 :(得分:0)

感谢您的回答但我已经解决了。对于令人困惑的问题,这只是一个错误。我忘记了给你们最重要的代码

昏暗的形式作为新的Formsomething

这是一个错误..我只是用实际的形式替换每一个表格,它工作得很好..我只是一个编码的初学者,所以我很抱歉和我的错误,但仍然感谢你答案