简体   繁体   English

DataGridView上的组合框

[英]Combobox on DataGridView

I have a combobox on a datagridview. 我在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. 我能够填充组合框的值,但是我无法做的是在加载datagridview时将数据库值填充到组合框“ text”中。

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. 我收到异常错误消息,说“ logs Update”不存在,但已加载到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. 这使我能够调用返回的db中datagridview的值作为组合框“文本”值,但将下拉项显示为另一个表中的项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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