DataGridView是否ComboBox列

时间:2017-10-02 15:26:20

标签: vb.net datagridview datagridcomboboxcolumn

我面对它似乎是一个非常简单的要求,我需要一个未绑定的组合框列来保存选项列表'是'并且没有'。在组合框中,“是”和“否”是分别对应于ValueMembers 1和0的DisplayMembers,而ValueMembers又是数据库表字段中保存的值。我在应用程序中广泛使用绑定的组合框列,并且非常熟悉构建它们,但是我不知道如何在datagridview应用程序中合并这个简单的列表功能。有人可以用一些示例代码让我摆脱困境。

感谢您的反馈Plutonix。 DGV绑定到基础表。希望以下代码可以让您了解我试图实现的目标。与此帖特别相关的代码是对PopulateUnboundComboBox的调用(comboBoxCol,{"是"," No"})。

Private Sub GetData()
    Const procName As String = "GetData()"

    Dim myValue As String = ""
    Dim myDataSource As Integer = 0

    Try

        'First clear the existing grid
        With Me.uxGrid
            If .ColumnCount > 0 Then
                'clear the grid
                ClearGrid(Me.uxGrid)
            End If
        End With

        _Logger.SendLog(Me.Name & "." & procName & " - Fetching device data.", NLog.LogLevel.Trace)

       'Get the data from the database
        If Not _dataSourceID = 0 Then
            _sqlStatement = "SELECT ID, TEXT, CATEGORY, MOUNTING, DATASOURCEID, TANALYSIS" & _
                              " FROM P_TBL_DEVICE WHERE DATASOURCEID = " & _dataSourceID
            _criteria = "WHERE DATASOURCEID = " & _dataSourceID
        Else
            _sqlStatement = ""
            _criteria = ""
        End If

        _myDeviceMngr = New DeviceManager(_currentDB, _userName, _myPwd)

        'Now get the latest data
        _myDataSet = _myDeviceMngr.GetData(_sqlStatement)

        _Logger.SendLog(Me.Name & "." & procName & " - Device data fetch completed.", NLog.LogLevel.Trace)

        'Update the display
        Call BuildGrid(_myDataSet, uxGrid)

    Catch ex As Exception

        Beep()
        MsgBox(ex.Message, MsgBoxStyle.Exclamation, System.Windows.Forms.Application.ProductName)
        _Logger.SendLog(ex.Message & ".  Thrown in module " & Me.Name.ToString & "." & procName, NLog.LogLevel.Error, ex)

    Finally

        Call System.GC.Collect()

    End Try

End Sub




Private Sub BuildGrid(ByVal myDataSet As DataSet, ByVal myGrid As DataGridView)

    Dim myTable As New System.Data.DataTable
    Dim myColCount As Integer
    Dim comboBoxCol As DataGridViewComboBoxColumn
    Dim readOnlyCellStyle As New DataGridViewCellStyle
    Dim textBoxCol As DataGridViewTextBoxColumn
    Dim gridObject As Object = Nothing

    Const procName As String = "BuildGrid"

    readOnlyCellStyle.ForeColor = Color.Gray

    Try

        _Logger.SendLog(Me.Name & "." & procName & " - Building device data grid.", NLog.LogLevel.Trace, Nothing)

        myTable = myDataSet.Tables(0)
        With myGrid

            'Now add the columns to the grid 
            Dim column As System.Data.DataColumn
            For Each column In myDataSet.Tables(0).Columns
                'We dont want to include the changedon and changedby fields in the grid build
                If column.ColumnName.IndexOf("CHANGED") = -1 Then


                    Select Case column.ColumnName
                        Case "DATASOURCEID"
                            gridObject = New Object
                            comboBoxCol = New DataGridViewComboBoxColumn
                            'First create the comboboxcolumn for the column
                            'Populate combobox
                            PopulateScadaSourceComboBoxCol(comboBoxCol)
                            comboBoxCol.DataPropertyName = "DATASOURCEID"
                            comboBoxCol.HeaderText = "SCADA SOURCE"
                            comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
                            comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
                            gridObject = comboBoxCol

                        Case "TEXT"
                            textBoxCol = New DataGridViewTextBoxColumn
                            gridObject = textBoxCol
                            'Now add the columns to the grid 
                            gridObject.DataPropertyName = column.ColumnName.ToString
                            gridObject.HeaderText = column.ColumnName.Replace("TEXT", "DEVICE")
                            gridObject.name = column.ColumnName.Replace("TEXT", "DEVICE")
                            gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells

                        Case "CATEGORY"
                            textBoxCol = New DataGridViewTextBoxColumn
                            gridObject = textBoxCol
                            'Now add the columns to the grid 
                            gridObject.DataPropertyName = column.ColumnName.ToString
                            gridObject.HeaderText = "CATEGORY"
                            gridObject.name = "CATEGORY"
                            gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells

                        Case "MOUNTING"
                            textBoxCol = New DataGridViewTextBoxColumn
                            gridObject = textBoxCol
                            'Now add the columns to the grid 
                            gridObject.DataPropertyName = column.ColumnName.ToString
                            gridObject.HeaderText = "MOUNTING"
                            gridObject.name = "MOUNTING"
                            gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells

                        Case "TANALYSIS"
                            comboBoxCol = New DataGridViewComboBoxColumn
                            'First create the comboboxcolumn for the column
                            'Populate combobox
                            PopulateUnboundComboBox(comboBoxCol, {"Yes", "No"})
                            comboBoxCol.DataPropertyName = "TANALYSIS"
                            comboBoxCol.HeaderText = "TELECONTROL ANALYSIS"
                            comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
                            comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
                            gridObject = comboBoxCol

                        Case Else
                            textBoxCol = New DataGridViewTextBoxColumn
                            gridObject = textBoxCol
                            'Now add the columns to the grid 
                            gridObject.DataPropertyName = column.ColumnName.ToString
                            gridObject.HeaderText = "ID"
                            gridObject.name = "ID"
                            gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells

                    End Select

                    'Now add the textbox columns to the grid 
                    'gridObject.DataPropertyName = column.ColumnName.ToString
                    'gridObject.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
                    .Columns.Add(gridObject)
                End If
            Next

            'Set grid default styles/values
            .Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
            .AllowUserToResizeColumns = True
            .AllowUserToResizeRows = True
            .AllowUserToAddRows = True
            .ReadOnly = True
            .AutoResizeRows()
            .ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText

            'Now bind the datatable to the DGV datasource property
            .DataSource = myTable

        End With

        _Logger.SendLog(Me.Name & "." & procName & " - Building of device data grid has been completed.", NLog.LogLevel.Trace, Nothing)

    Catch ex As Exception
        Throw
    Finally
        textBoxCol = Nothing
        comboBoxCol = Nothing
        readOnlyCellStyle = Nothing
        GC.Collect()
    End Try

End Sub

 Private Sub PopulateUnboundComboBox(ByVal comboBoxCol As DataGridViewComboBoxColumn, byval valList() as string)
      comboBoxCol.Name = "TANALYSIS"
      comboBoxCol.DataSource = valList

      comboBoxCol.ValueMember = ???
      comboBoxCol.DisplayMember = ???

 End Sub

EDITED //

好的,我已经取得了一些进展,似乎已经解决了我的问题。剩下的一个问题是DGV中的列正在显示“值会员”。 (1或0)而不是' DisplayMember' (是还是不是)。我已经在组合框列定义中检查了valuemember和displaymember属性,并且它们似乎已正确设置。以下是相关代码:

摘自上面列出的原始代码发布

 ...

 Case "TANALYSIS"
      gridObject = New Object
      comboBoxCol = New DataGridViewComboBoxColumn
      'First create the comboboxcolumn for the column
      'Populate combobox

      Dim item As New CBOItem
      Dim itemList As New CBOItemList
      item.ValueMember = 0
      item.DisplayMember = "No"
      itemList.Add(item)
      item = New CBOItem
      item.ValueMember = 1
      item.DisplayMember = "Yes"
      itemList.Add(item)

      PopulateComboBox(comboBoxCol, itemList)
      comboBoxCol.DataPropertyName = "TANALYSIS"
      comboBoxCol.HeaderText = "TELECONTROL ANALYSIS"
      comboBoxCol.AutoSizeMode = DataGridViewAutoSizeColumnsMode.AllCells
      comboBoxCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
      gridObject = comboBoxCol
 ...

Private Sub PopulateComboBox(ByRef comboBoxCol As DataGridViewComboBoxColumn, ByVal itemList As CBOItemList)

    Dim tbl As DataTable = New DataTable
    Dim row As DataRow

    tbl.Columns.Add("ValueMember")
    tbl.Columns.Add("DisplayMember")
    row = tbl.NewRow
    row.Item("ValueMember") = itemList(0).ValueMember
    row.Item("DisplayMember") = itemList(0).DisplayMember
    tbl.Rows.Add(row)
    row = tbl.NewRow
    row.Item("ValueMember") = itemList(1).ValueMember
    row.Item("DisplayMember") = itemList(1).DisplayMember
    tbl.Rows.Add(row)

    comboBoxCol.ValueMember = tbl.Columns("ValueMember").ToString
    comboBoxCol.DisplayMember = tbl.Columns("DisplayMember").ToString
    comboBoxCol.DataSource = tbl

End Sub

亲切的问候 保罗J.

0 个答案:

没有答案