繁体   English   中英

在vb.net中填充datagridview组合框

[英]Populate combobox of datagridview in vb.net

我正在尝试填充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
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

暂无
暂无

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

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