【问题标题】:Populate combobox of datagridview in vb.net在 vb.net 中填充 datagridview 的组合框
【发布时间】:2017-01-22 07:00:57
【问题描述】:

我正在尝试填充 datagridview 的组合框,但没有得到任何结果 这是我的代码

 Private Sub loadcombo()
    Dim cmb1 As DataGridViewComboBoxColumn = CType(dgventry.Columns("Party"), DataGridViewComboBoxColumn)
    Try
        Dim adapter As New OleDbDataAdapter
        con = New OleDbConnection(connectionString)
        con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet
        sqlstr = "SELECT LedgerTab.lname, LedgerTab.lcode FROM LedgerTab WHERE ((LedgerTab.lgcode='LG27' OR LedgerTab.lgcode = 'LG4' OR LedgerTab.lgcode = 'LG33' OR LedgerTab.lgcode = 'LG23' OR LedgerTab.lgcode = 'LG26' ) and LedgerTab.comcode = @comcode and LedgerTab.isdeleted='N') ORDER BY LedgerTab.lname ASC"
        ds.Tables.Add(dt)
        adapter.SelectCommand = New OleDbCommand(sqlstr, con)
        adapter.SelectCommand.Parameters.AddWithValue("@comcode", compcode)
        adapter.Fill(dt)
        cmb1.DataSource = ds.Tables(0)
        cmb1.ValueMember = "lcode"
        cmb1.DisplayMember = "lname"
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

【问题讨论】:

    标签: vb.net datagridview combobox


    【解决方案1】:
    Private Sub loadcombo()
        Dim cmb1 As New DataGridViewComboBoxColumn
        Try
            Dim adapter As New OleDbDataAdapter
            con = New OleDbConnection(connectionString)
            '   no need to open, .FILL method automatically does this for you
            'con.Open()        
            Dim ds As New DataSet
            sqlstr = "SELECT LedgerTab.lname, LedgerTab.lcode FROM LedgerTab WHERE ((LedgerTab.lgcode='LG27' OR LedgerTab.lgcode = 'LG4' OR LedgerTab.lgcode = 'LG33' OR LedgerTab.lgcode = 'LG23' OR LedgerTab.lgcode = 'LG26' ) and LedgerTab.comcode = @comcode and LedgerTab.isdeleted='N') ORDER BY LedgerTab.lname ASC"        
            adapter.SelectCommand = New OleDbCommand(sqlstr, con)
            adapter.SelectCommand.Parameters.AddWithValue("@comcode", compcode)
            adapter.Fill(ds)        
            '   no need to close, connection has already been closed by FILL
            'con.Close()
    
            cmb1.DataSource = ds.Tables(0)
            cmb1.ValueMember = "lcode"
            cmb1.DisplayMember = "lname"
    
            '   lcode here refers to the column in dgvEntry's datasource.
            '   it must have the same data type as the lcode in LedgerTab table
            cmb1.Name = "lcode" 
            cmb1.DataPropertyName = "lcode"
    
            dgvEntry.Columns.Add(cmb1)
    
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多