简体   繁体   中英

Combobox on DataGridView

I have a combobox on a datagridview. I am able to populate the values of the combobox, but what I am failing to do is to populate the data base value into the combobox "text" when the datagridview is loading.

I hope this makes sense. Basically I just want the data returned to reflect in the combobox Display.

Your assistance will be highly appreciated.

 Dim cbo As DataGridViewComboBoxColumn
        If GlobalVariables.UsrSite = 0 Then
            SQL.ReadQuery("Select LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
            SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down] 
            from UDOData where status = 'Down' order by [Days Down] Desc")
        Else
            SQL.ReadQuery("Select LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
            SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down] from UDOData 
            Where SiteID = " & GlobalVariables.UsrSite & " and Status = 'Down' order by [Days Down] Desc")
        End If
        dgvUDO.DataSource = SQL.SQLDS.Tables(0)

        cbo.DataSource = SQL.SQLDS.Tables(0)
        cbo.DisplayMember = "Logs Update"
        cbo.ValueMember = "Logs Update"
        cbo.DataPropertyName = "Logs Update"

I get exception error saying "logs Update" does not exist, but it loads in the datagridview.

在此处输入图片说明

I ended up doing this, which worked for me.

Dim cbo As DataGridViewComboBoxColumn
        cbo = dgvUDO.Columns("cboLogsUDO")
        Dim AltDb As New DataTable

        SQL.ReadQuery("Select Descrip from dbo.UDOLogs")
        AltDb = SQL.SQLDS.Tables(0)
        'LOAD DATA GRID VIEW DATA
        If GlobalVariables.UsrSite = 0 Then
            SQL.ReadQuery("Select ID, LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
            SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down] 
            from UDOData where status = 'Down' order by [Days Down] Desc")
        Else
            SQL.ReadQuery("Select ID, LogsReason as [Logs Update], IncNo as [Number], SiteName As [Site Name], FleetNo As [Fleet No], FleetType As [Fleet Type], Location,
            SMR as [Hours], TimeDown As [Down Time], EstTimeUp as [Est Repair], DateDiff(d, TimeDown, GetDate()) as [Days Down] from UDOData 
            Where SiteID = " & GlobalVariables.UsrSite & " and Status = 'Down' order by [Days Down] Desc")
        End If

        'SET DATA GRIDVIEW DATA SOURCE
        dt = SQL.SQLDS.Tables(0)
        dgvUDO.DataSource = dt
        dgvUDO.Rows(0).Selected = True

        'LOAD UPDATE COMMAND FOR DATA GRIDVIEW CHANGES
        SQL.SQLDA.UpdateCommand = New SqlClient.SqlCommandBuilder(SQL.SQLDA).GetUpdateCommand

        'HIDE UNWANTED COLUMNS
        dgvUDO.Columns("Logs Update").Visible = False
        dgvUDO.Columns("ID").Visible = False


        'SET COMBOBOX ATTRIBUTES
        cbo.DataSource = AltDb
        cbo.DisplayMember = "Descrip"
        cbo.ValueMember = "Descrip"
        cbo.DataPropertyName = "Logs Update"

This gives me the ability to call the value in the returned db for the datagridview as the combobox "text" value, but show the drop down items as the items in another table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM