DataGridView上的Combobox

时间:2017-03-14 22:16:27

标签: vb.net datagridview combobox

我在datagridview上有一个组合框。我能够填充组合框的值,但是我没有做的是在加载datagridview时将数据库值填充到组合框“text”中。

我希望这是有道理的。基本上我只想让返回的数据反映在组合框显示中。

我们非常感谢您的协助。

 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"

我收到异常错误,说“日志更新”不存在,但会在datagridview中加载。

enter image description here

1 个答案:

答案 0 :(得分:0)

我最终做了这件事,这对我有用。

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"

这使我能够将datagridview的返回数据库中的值作为组合框" text" value,但将下拉项显示为另一个表中的项。