2016-09-14 77 views
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" 
+0

運行一個'SELECT'查詢只從表中得到你需要的一列,然後用結果填充一個DataTable,並設置'combobox.DataSource = DataTable' – David

+0

開始爲它創建一個'DataGridViewComboboxColumn'。使用項目的數據源或在設計師中設計它 – Plutonix

+0

你能告訴我怎麼做嗎? –

回答

0
This how I did it: 

Sub FillGrid() 
    Dim cbb As New DataGridViewComboBoxColumn() With {.HeaderText ="العملة"} 
    cbb.Items.Add("شيقل.ج") 
    cbb.Items.Add("د.اردني") 
    cbb.Items.Add("د.امريكي") 
    cbb.Items.Add("يورو") 
    cbb.Items.Add("ر.صيني") 
    'ADD BUTTON 
    Dim btnDelete As New DataGridViewButtonColumn() 
    Dim btnImage As New DataGridViewButtonColumn() 

    MetroGrid2.Columns.Insert(0, btnDelete) 
    MetroGrid2.Columns.Add("", "المبلغ المرسل") 
    MetroGrid2.Columns.Insert(2, cbb) 
    MetroGrid2.Columns.Add("", "العمولة") 
    MetroGrid2.Columns.Add("", "أسم المستقبل") 
    MetroGrid2.Columns.Add("", "هاتف المستقبل") 
    MetroGrid2.Columns.Add("", "البلد المستقبل") 
    MetroGrid2.Columns.Add("", "أسم المرسل") 
    MetroGrid2.Columns.Add("", "هاتف المرسل") 
    MetroGrid2.Columns.Add("", "الشركة المستقبلة") 
    MetroGrid2.Columns.Add("", "تاريخ التسجيل") 
    MetroGrid2.Columns.Insert(11, btnImage) 
    MetroGrid2.Columns.Add("", "الملاحظات") 
    MetroGrid2.Columns.Add("", "ID") 
    MetroGrid2.Columns(13).Visible = False 

    Const sql As String = "SELECT * FROM money_sent" 
    Dim adt As New SqlDataAdapter(sql, Conn) 
    Dim dt As New DataTable 
    adt.Fill(dt) 
    Dim i As Integer 
    For i = 0 To dt.Rows.Count - 1 
     MetroGrid2.Rows.Add() 
     MetroGrid2.Rows(i).Cells(1).Value = dt.Rows(i).ItemArray(1) 
     Dim cell As DataGridViewComboBoxCell = DirectCast(MetroGrid2.Rows(i).Cells(2), DataGridViewComboBoxCell) 
     cell.Value = dt.Rows(i).ItemArray(2) 
     MetroGrid2.Rows(i).Cells(3).Value = dt.Rows(i).ItemArray(3) 
     MetroGrid2.Rows(i).Cells(4).Value = dt.Rows(i).ItemArray(4) 
     'DataGridView1.Rows(i).Cells(4).Value = "DELETE" 
     MetroGrid2.Rows(i).Cells(5).Value = dt.Rows(i).ItemArray(5) 
     MetroGrid2.Rows(i).Cells(6).Value = dt.Rows(i).ItemArray(6) 
     MetroGrid2.Rows(i).Cells(7).Value = dt.Rows(i).ItemArray(7) 
     MetroGrid2.Rows(i).Cells(8).Value = dt.Rows(i).ItemArray(8) 
     MetroGrid2.Rows(i).Cells(9).Value = dt.Rows(i).ItemArray(9) 
     MetroGrid2.Rows(i).Cells(10).Value = dt.Rows(i).ItemArray(10) 
     MetroGrid2.Rows(i).Cells(12).Value = dt.Rows(i).ItemArray(12) 
     MetroGrid2.Rows(i).Cells(13).Value = dt.Rows(i).ItemArray(0) 
    Next 

End Sub 
相關問題