0
我是DataGridView中的新成員我通常使用列表視圖 我的程序從sql服務器中檢索數據並在datagridview中顯示它 所以我如何讓列之一是combobox並添加子項目,並顯示來自數據庫的組合框中的當前值?在datagridview組合框中顯示來自sql server的數據
這是我的代碼來顯示數據:
Public Sub showGV()
Using cmd As New SqlCommand("SELECT * FROM money_sent", Conn)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
'Set AutoGenerateColumns False
MetroGrid2.AutoGenerateColumns = False
'Set Columns Count
MetroGrid2.ColumnCount = 12
'Add Columns
Dim bc As New DataGridViewButtonColumn
bc.Tag = False
bc.Text = "Delete"
bc.Name = ""
bc.Width = 25
'MetroGrid1.Columns.Add(bc)
MetroGrid2.Columns.Insert(0, bc)
MetroGrid2.Columns(1).Name = "smoney"
MetroGrid2.Columns(1).HeaderText = "المبلق المرسل"
MetroGrid2.Columns(1).DataPropertyName = "smoney"
MetroGrid2.Columns(2).Name = "moneyt"
MetroGrid2.Columns(2).HeaderText = "العملة"
MetroGrid2.Columns(2).DataPropertyName = "moneyt"
MetroGrid2.Columns(3).Name = "commission"
MetroGrid2.Columns(3).HeaderText = "العمولة"
MetroGrid2.Columns(3).DataPropertyName = "commission"
MetroGrid2.Columns(4).Name = "rname"
MetroGrid2.Columns(4).HeaderText = "أسم المستقبل"
MetroGrid2.Columns(4).DataPropertyName = "rname"
MetroGrid2.Columns(5).Name = "rphone"
MetroGrid2.Columns(5).HeaderText = "هاتف المستقبل"
MetroGrid2.Columns(5).DataPropertyName = "rphone"
MetroGrid2.Columns(6).Name = "rcountry"
MetroGrid2.Columns(6).HeaderText = "البلد المستقيل"
MetroGrid2.Columns(6).DataPropertyName = "rcountry"
MetroGrid2.Columns(7).Name = "sname"
MetroGrid2.Columns(7).HeaderText = "أسم المستقبل"
MetroGrid2.Columns(7).DataPropertyName = "sname"
MetroGrid2.Columns(8).Name = "sphone"
MetroGrid2.Columns(8).HeaderText = "هاتف المستقبل"
MetroGrid2.Columns(8).DataPropertyName = "sphone"
MetroGrid2.Columns(9).Name = "comname"
MetroGrid2.Columns(9).HeaderText = "الشركة المستقبلة"
MetroGrid2.Columns(9).DataPropertyName = "comname"
MetroGrid2.Columns(10).Name = "date"
MetroGrid2.Columns(10).HeaderText = "تاريخ الارسال"
MetroGrid2.Columns(10).DataPropertyName = "date"
MetroGrid2.Columns(11).Name = "note"
MetroGrid2.Columns(11).HeaderText = "الملاحظات"
MetroGrid2.Columns(11).DataPropertyName = "note"
MetroGrid2.Columns(12).Name = "id"
MetroGrid2.Columns(12).HeaderText = "id"
MetroGrid2.Columns(12).DataPropertyName = "id"
MetroGrid2.DataSource = dt
Dim imageb As New DataGridViewButtonColumn
imageb.Tag = False
imageb.Text = "image"
imageb.Name = ""
imageb.Width = 25
'MetroGrid1.Columns.Add(bc)
MetroGrid2.Columns.Insert(10, imageb)
Me.MetroGrid2.Columns("id").Visible = False
End Using
End Using
End Using
End Sub
,這是我希望把它組合框列的列:
MetroGrid2.Columns(2).Name = "moneyt"
MetroGrid2.Columns(2).HeaderText = "العملة"
MetroGrid2.Columns(2).DataPropertyName = "moneyt"
運行一個'SELECT'查詢只從表中得到你需要的一列,然後用結果填充一個DataTable,並設置'combobox.DataSource = DataTable' – David
開始爲它創建一個'DataGridViewComboboxColumn'。使用項目的數據源或在設計師中設計它 – Plutonix
你能告訴我怎麼做嗎? –