简体   繁体   中英

Combobox population does not appear in DataGridView in vb.net

Combobox population does not appear in DataGridView because of the function of DataTable. below link the previous code running normally with the combobox population. please recommend the best solution. I have also coded in my post so it doesn't miss communication Thanks

link previous post

Private _dt As DataTable
'update code PopulateComboBox in form load
  Public Sub New()
            If Me.IsInDesignMode() Then
                Return
            End If
            InitializeComponent()
            Me.PopulateComboBox()
            fillDataGridView1()
        End Sub
Private Function GetAndFillDataTable() As DataTable
    Dim dt As New DataTable()
    Dim query As String = "SELECT NOD,ITM,CIA,DPR,QTY FROM RSD WHERE QTY > 0 AND PNM=@PNM"
    Using con As OleDbConnection = New OleDbConnection(GetConnectionString)
        Using cmd As OleDbCommand = New OleDbCommand(query, con)
            cmd.Parameters.AddWithValue("@PNM", ComboBox1.SelectedValue)
            Using da As New OleDbDataAdapter(cmd)
                da.Fill(dt)
                da.Dispose()
                Dim totalColumn As New DataColumn()
                totalColumn.DataType = System.Type.GetType("System.Double")
                totalColumn.ColumnName = "Total"
                totalColumn.Expression = "[CIA]*[QTY]*(1-[DPR]/100)"
                dt.Columns.Add(totalColumn)
                Return dt
            End Using
        End Using
    End Using
End Function

Private Sub FillDataGridView1()
    If (_dt Is Nothing) Then
        _dt = GetAndFillDataTable()
    End If
    grid.DataSource = _dt
    grid.Refresh()
End Sub
Private Sub PopulateComboBox()
Dim dt As New DataTable()
 Dim query As String = "SELECT DISTINCT PNM FROM RSD UNION SELECT DISTINCT PNM FROM RSG ORDER BY PNM"
            Try
                dt = New DataTable
                Using con As OleDbConnection = New OleDbConnection(GetConnectionString)
                    Using sda As OleDbDataAdapter = New OleDbDataAdapter(query, con)
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
 row(0) = ""
dt.Rows.InsertAt(row, 0)
  ComboBox1.DataSource = dt
                        ComboBox1.DisplayMember = "PNM"
                        ComboBox1.ValueMember = "PNM"
 End Using
                End Using
            Catch myerror As OleDbException
                MessageBox.Show("Error: " & myerror.Message)
            Finally
            End Try
'update code combobox selectionchangecommited for fillDataGridView1
 Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
fillDataGridView1()
 End Sub

You are setting your datasource = _dt in PopulateComboBox however the data.table you built in your function is called dt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM