将绑定源转换为 VB.NET 中的 datagridview 时,对象引用未设置为组合框中对象的实例

问题描述 投票:0回答:1

我尝试将绑定源转换为 datagridview 但出现错误,问题出在组合框绑定中。下面,如果我使用第一个代码选项,就没有问题错误,但是如果我使用第二个代码选项,就会出现问题错误

object reference not set to an instance of an object
。我怎样才能继续使用第二个代码选项,请指导我。

谢谢

第一个代码选项

Public Class Form2
    Private bindingSource As BindingSource = Nothing
Public Sub New()
        InitializeComponent()
        BindcomboboxJob1()
        ComboBox1.DropDownHeight = 80
        ComboBox1.Text = "Job1"
    End Sub
Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
    End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim job1 = ComboBox1.Text.Trim()
 Try
            Dim new_product = New People With {
            .Transno = TextBox1.Text,
            .Username = TextBox2.Text,
            .Job1 = job1}
            Dim bs = TryCast(bindingSource.DataSource, BindingList(Of People))
  bs.Add(new_product)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Private Sub BindcomboboxJob1()
        If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource
        Cursor.Current = Cursors.Default
    End Sub
End Class

第二个代码选项

Public Class Form2
    Private bindingSource As BindingSource = Nothing
 Public Sub New()
        InitializeComponent()
        ComboBox1.DropDownHeight = 80
        ComboBox1.Text = "Job1"
Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
    End Sub
'error in the code below when using the button event to add to the datagridview

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim job1 = ComboBox1.Text.Trim()
 Try
            Dim new_product = New People With {
            .Transno = TextBox1.Text,
            .Username = TextBox2.Text,
            .Job1 = job1}
            Dim bs = TryCast(bindingSource.DataSource, BindingList(Of People))
  bs.Add(new_product)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Private Sub BindcomboboxJob1()
        If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource
        Cursor.Current = Cursors.Default
    End Sub
 Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown

        BindcomboboxJob1()

    End Sub

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown

        BindcomboboxJob1()
    End Sub
End Class
vb.net combobox binding bindingsource bindinglist
1个回答
0
投票

这要归功于@dr.null 的帮助和建议。我非常感谢你

Public Class Form2
    Private bindingSource As BindingSource = Nothing
    Private bindingSource1 As BindingSource = Nothing
 Public Sub New()
        InitializeComponent()
        ComboBox1.DropDownHeight = 80
        ComboBox1.Text = "Job1"
Public Sub New(Transno As People)
        Me.New
        bindingSource = New BindingSource With {.DataSource = New BindingList(Of People)(CType(pservice.GetPeopleview(Transno.Transno), IList(Of People)))}
        DataGridView1.DataSource = bindingSource
    End Sub
'error in the code below when using the button event to add to the datagridview

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim job1 = ComboBox1.Text.Trim()
 Try
            Dim new_product = New People With {
            .Transno = TextBox1.Text,
            .Username = TextBox2.Text,
            .Job1 = job1}
            Dim bs = TryCast(bindingSource.DataSource, BindingList(Of People))
  bs.Add(new_product)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Private Sub BindcomboboxJob1()
        If ComboBox1.DataSource IsNot Nothing Then Return
        Cursor.Current = Cursors.WaitCursor
        ComboBox1.DisplayMember = "Job1"
        ComboBox1.ValueMember = "Job1"
        bindingSource1 = New BindingSource With {.DataSource = New BindingList(Of Job1)(CType(pservice.GetByJob1, IList(Of Job1)))}
        ComboBox1.DataSource = bindingSource1
        Cursor.Current = Cursors.Default
    End Sub
 Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown

        BindcomboboxJob1()

    End Sub

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown

        BindcomboboxJob1()
    End Sub
End Class

© www.soinside.com 2019 - 2024. All rights reserved.