设置嵌入在datagridview单元格中的Combobox的文本颜色

时间:2015-08-06 17:10:06

标签: vb.net datagridview combobox

我有一个列很少的网格。我已经提到了一个条件,就像列Amount的Cell值一样。 0然后将该行的文本颜色更改为灰色。现在我的一个专栏有#34; DataGridViewComboBox"嵌入其中。现在当满足< 0条件时,该行中所有其他列的文本变为灰色,除了这" DataGridViewComboBox"列。

我想知道如何将Combobox所选文本的颜色设置为灰色?

Private Function GetComboBoxColumn_Category() As DataGridViewComboBoxColumn
    Dim ColCombo As New DataGridViewComboBoxColumn
    Try
        Using Connection = GetConnection()
            da = New SqlDataAdapter("SELECT hKey 'iCategory', sCategory FROM tbl_CategoryList WHERE IsObsolete = 0", Connection)
            dt = New DataTable
            da.Fill(dt)
        End Using
        ColCombo.DataPropertyName = "iCategory"
        ColCombo.HeaderText = "Category"
        ColCombo.Name = "iCategory"
        ColCombo.DataSource = dt
        ColCombo.ValueMember = "iCategory"
        ColCombo.DisplayMember = "sCategory"
        ColCombo.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
    Catch ex As Exception
        ImpensaAlert(ex.Message, MsgBoxStyle.Critical)
    End Try

    Return ColCombo
End Function '1

Private Sub PopulateExpenditureDetailGrid()
    Dim TextBoxCell As DataGridViewTextBoxCell
    Dim strSQL As String = ""
    Dim dc_Category As DataGridViewComboBoxColumn
    Dim dc_DelChk As New DataGridViewCheckBoxColumn

    Try
        DataGridExpDet.DataSource = Nothing
        'DataGridExpDet.Columns.Clear()

        Label15.Text = "Getting Detail Records..."
        Application.DoEvents()

        StrClosedYrs = BuildOpenOrClosedYrsStr(1) 'List Of Closed Years
        dc_Category = GetComboBoxColumn_Category()
        DataGridExpDet.Columns.Add(dc_Category)


Private Sub DataGridExpDet_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridExpDet.CellLeave


    If DataGridExpDet.CurrentCell.ColumnIndex = DataGridExpDet.Columns("Amount").Index And Not String.IsNullOrEmpty(DataGridExpDet(DataGridExpDet.CurrentCell.ColumnIndex, DataGridExpDet.CurrentCell.RowIndex).EditedFormattedValue) Then
        If DataGridExpDet(DataGridExpDet.CurrentCell.ColumnIndex, DataGridExpDet.CurrentCell.RowIndex).EditedFormattedValue < 0 Then
            DataGridExpDet.Rows(DataGridExpDet.CurrentCell.RowIndex).DefaultCellStyle.ForeColor = Color.Gray
        End If
    End If
End Sub

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

通常,要更改文本的颜色,请更改.forecolor

force.gravity = function(x) {
  if (!arguments.length) return gravity;
  gravity = +x;
  return force;
};

将更改位于组合框中的所有文本的颜色。

但是,我不认为datagridview组合框默认具有此选项。

每当我这样做自定义工作时,我通常会使用RadComboBox。他们有大量的自定义选项,可以让您完成所需的任务。

修改

啊,哈哈,找到了! - 在这里go

显然你会像普通的组合框一样进行编辑。

答案 1 :(得分:0)

您有两种选择。一个很容易,另一个很容易。

  1. 为列添加Flat样式。这将改变ComboBox看起来的方式。但如果您对此感到满意,那么这就是简单的方法。只需将以下行添加到DataGridViewComboBoxColumn创建中:

    ColCombo.FlatStyle = FlatStyle.Flat
    
  2. 以正确的颜色手动绘制ComboBox文字。为此,除了代码之外,还要处理DataGridView.CellPaintingDataGridView.EditingControlShowing事件。此方法较长,但如果您想保持ComboBox的外观,则此选项应该可以正常运行。

    ' If the cell is a ComboBox cell: Grab the row ForeColor,
    ' Paint the cell as usual minus the text, then manually
    ' Paint the text in the correct color.
    ' This displays the cell text in the correct color when the cell is not in edit mode.
    Private Sub DataGridExpDet_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridExpDet.CellPainting
        If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then
            If TypeOf DataGridExpDet.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewComboBoxCell Then
                Dim cell As DataGridViewComboBoxCell = DataGridExpDet.Rows(e.RowIndex).Cells(e.ColumnIndex)
                Dim row As DataGridViewRow = DataGridExpDet.Rows(e.RowIndex)
                Dim color As Color = If(row.DefaultCellStyle.ForeColor.Name = "0", color.Black, row.DefaultCellStyle.ForeColor)
    
                Using brush = New SolidBrush(color)
                    Using format = New StringFormat()
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.Background)
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.Border)
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentBackground)
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.ErrorIcon)
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus)
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.SelectionBackground)
    
                        'Don't do the following one - that's what we're overriding.
                        'e.Paint(e.ClipBounds, DataGridViewPaintParts.ContentForeground)
    
                        format.LineAlignment = StringAlignment.Center
    
                        e.Graphics.DrawString(cell.FormattedValue, e.CellStyle.Font, brush, e.CellBounds, format)
                        e.Handled = True
                    End Using
                End Using
            End If
        End If
    End Sub
    
    ' Handle the ComboBox control's DrawItem event when showing.
    ' This draws the selected item and item options' texts in the correct color when in edit mode.
    Private Sub DataGridExpDet_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridExpDet.EditingControlShowing
        If TypeOf e.Control Is ComboBox Then
            Dim cb As ComboBox = TryCast(e.Control, ComboBox)
            cb.DrawMode = DrawMode.OwnerDrawFixed
    
            RemoveHandler cb.DrawItem, AddressOf GridComboBox_DrawItem
            AddHandler cb.DrawItem, AddressOf GridComboBox_DrawItem
        End If
    End Sub
    
    ' Draw the background and focus as normal, then manually
    ' draw the text values from the indexed datasource row of the
    ' corresponding DisplayValue column.
    Private Sub GridComboBox_DrawItem(sender As Object, e As DrawItemEventArgs)
        e.DrawBackground()
        e.DrawFocusRectangle()
    
        Dim cb As ComboBox = TryCast(sender, ComboBox)
        Dim dt As DataTable = TryCast(cb.DataSource, DataTable)
    
        Using brush = New SolidBrush(cb.ForeColor)
            e.Graphics.DrawString(dt.Rows(e.Index).Item("sCategory"), e.Font, brush, e.Bounds)
        End Using
    End Sub