简体   繁体   English

Combobox vb.net DataGridView中没有出现人口

[英]Combobox population does not appear in DataGridView in vb.net

Combobox population does not appear in DataGridView because of the function of DataTable. Combobox人口没有出现在DataGridView中,因为DataTable的function。 below link the previous code running normally with the combobox population.下面链接以前的代码正常运行与 combobox 人口。 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.您正在 PopulateComboBox 中设置数据源 = _dt,但是您在 function 中构建的 data.table 称为 dt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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