我有一个从Access数据库填充的datagridview。我能够将主表的前两列填充到“ TextBoxColumn1”和“ TextBoxColumn2”中。我遇到的问题是将填充组合框(cmbCol1)的联接表。
如何从联接表中完全填充组合框,然后根据其ID选择项目?
SQL:
选择ProdInv.lUniqueID AS ProdID,将ProdInv.progname作为ProgName, FeatureType.luniqueID AS FeatID,FeatureType.strType AS FeatType, ProdInv.Filename,prodinv.Mon,prodinv.Tue,prodinv.wed,prodinv.Thu, prodinv.fri,prodinv.sat,prodinv.sun,prodinv.chkplay, prodinv.CartFileName,prodinv.TrnsfrTIme,prodinv.AirTime, 计划赞助者,计划时间表,部委联系方式, prodinv.Producer,prodinv.ProductionContact, distributor.strDistributor,prodinv.DistributionContact, automantype.strtype,WebFTPType.strtype 从 ((((((ProdInv左联接FeatureType在FeatureType上。[lUniqueID] = ProdInv.FeatureTypeID) LEFT JOIN分发服务器在分发服务器上。[lUniqueID] = ProdInv.distributor_id) LEFT JOIN自动模式ON自动模式。[lUniqueID] = ProdInv.automanid) 左联接WebFTPType ON WebFTPType。[lUniqueID] = ProdInv.webid) LEFT JOIN triggerType ON triggerType。[lUniqueID] = ProdInv.trigger_id
Dim cn As New OleDbConnection(strConn)
cn.Open()
ds = New DataSet
da.Fill(ds, "ProdInv")
cn.Close()
dgrMain.AutoGenerateColumns = False
dgrMain.DataSource = ds.Tables("ProdInv").DefaultView
Dim TextBoxColumn1 As New DataGridViewTextBoxColumn
TextBoxColumn1.HeaderText = "ID"
TextBoxColumn1.Width = 50
TextBoxColumn1.DataPropertyName = "ProdID"
Dim TextBoxColumn2 As New DataGridViewTextBoxColumn
TextBoxColumn2.HeaderText = "Program Name"
TextBoxColumn2.Width = 200
TextBoxColumn2.DataPropertyName = "ProgName"
Dim cmbCol1 As New DataGridViewComboBoxColumn
With cmbCol1
.HeaderText = "Feature Type"
.DataPropertyName = "FeatureType"
.DisplayMember = "strType"
End With
dgrMain.Columns.Add(TextBoxColumn1)
dgrMain.Columns.Add(TextBoxColumn2)
dgrMain.Columns.Add(cmbCol1)
更新 我意识到在将初始值加载到datagridview时,我试图同时做两件事。
现在,而不是我现在所做的所有联接: 选择*从ProdInv
我发现,如果将它分成两部分,它似乎可以正常工作(请继续阅读):
第1部分 这是加载表单时调用的代码函数:
Private Sub FillCombobox(strHeaderText As String)
Dim cboBox As New DataGridViewComboBoxColumn
With cboBox
.HeaderText = strHeaderText
.DataPropertyName = "strType"
.DataSource = Populate("select * from featuretype")
.ValueMember = "strType"
End With
dgrMain.Columns.Add(cboBox)
End Sub
Private Function Populate(ByVal sqlCommand As String) As DataTable
Dim da = New OleDbDataAdapter(sqlCommand, strConn)
Dim con As New OleDbConnection(strConn)
con.Open()
Dim command As OleDbCommand = New OleDbCommand(sqlCommand, con)
da.SelectCommand = command
Dim dt As DataTable = New DataTable()
da.Fill(dt)
con.Close()
Return dt
End Function
第2部分 当我在表单上放置一个按钮并在表单加载后 按下该按钮时,此部分仅适用于 。
Private Sub SelectItemInCbobox(strSQL As String, nColNum As Integer)
'Dim SQL As String = "Select FeatureTypeID (Select strType From FeatureType where ProdInv.FeatureTypeID = FeatureType.lUniqueid and Prodinv.lUniqueid) from prodinv"
Dim nRowIndex As Integer = 0
'Dim nColNum As Integer = 2
'
Dim dt As DataTable = Populate(strSQL)
Try
Do While nRowIndex < dt.Rows.Count
If dt.Rows(nRowIndex).Item(0).ToString <> String.Empty Then
dgrMain.Rows(nRowIndex).Cells(nColNum).Value = dt.Rows(nRowIndex).Item(COLUMN_0).ToString
End If
nRowIndex += 1
Loop
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
当我在表单加载期间尝试调用此方法时,它不起作用。在调试过程中,它可以完美地完成本节的操作,但仍不会在每个组合框中显示该值。
任何想法都将不胜感激,尤其是为什么它以一种方式起作用而不以另一种方式起作用。
答案 0 :(得分:0)
组合框需要分别填充要素并绑定到网格。
Private cmbCol1 As DataGridViewComboBoxColumn
Private dtFeatureTypes as New DataTable
dtFeatureTypes = you need to get the list of features to display in the combobox
cmbCol1 = dgrMain.Columns("FeatureType")
With cmbCol1
.HeaderText = "Feature Type"
.DataPropertyName = "FeatureType"
.DisplayMember = "strType"
.ValueMember = "idType" with the values
.DataSource = dtFeatureTypes
End With
答案 1 :(得分:0)
要直接用查询填充组合框,您可以这样操作:
dim sqlStatement as String
sqlStatement = "SELECT [...] FROM [...] JOIN " 'your SQL statement here
cboBox.RowSourceType = "Table/Query"
cboBox.RowSource = sqlStatement
但是您也可以将名称作为Access中设计的查询的字符串传递。
答案 2 :(得分:0)
我以为我正在调用例程以在form_load中填充表单,但是它在以下位置:
Public Sub New()
InitializeComponent()
GetData()
End Sub
将我的GetData()移到form_load中后,它就可以正常工作(请参见上文,了解我之前发布的内容的全部详细信息)。