在datagridview组合框列中显示来自sql server的数据

时间:2016-09-14 16:09:06

标签: vb.net datagridview

我是DataGridView中的新手我通常使用列表视图 我的程序从sql server检索数据并在datagridview中显示它 那么我怎样才能使其中一个列成为组合框并为其添加子项并显示来自数据库的组合框中的当前值?

这是我显示数据的代码:

   MetroGrid2.Columns(2).Name = "moneyt"
   MetroGrid2.Columns(2).HeaderText = "العملة"
   MetroGrid2.Columns(2).DataPropertyName = "moneyt"

这是我想让它成为组合框列的专栏:

android {
    productFlavors {
        flavorGermany {}
    }
}

1 个答案:

答案 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