我正在尝试填充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
答案 0 :(得分:0)
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