设置嵌入在datagridview单元格中的Combobox的文本颜色 [英] Set the text color of Combobox embedded in datagridview cell
问题描述
我有一个网格与几个列。我已经提到了一个条件, 0,然后将该行的文本颜色更改为灰色。现在我的一个列有DataGridViewComboBox嵌入它。现在当满足<0条件时,该行中所有其他列的文本将变为灰色,但此DataGridViewComboBox列除外。
我想知道如何设置
私有函数GetComboBoxColumn_Category()As DataGridViewComboBoxColumn
Dim ColCombo As New DataGridViewComboBoxColumn
Try
使用Connection = GetConnection()
da =新的SqlDataAdapter(SELECT hKey'iCategory',sCategory FROM tbl_CategoryList WHERE IsObsolete = 0,Connection)
dt = New DataTable
da.Fill(dt)
结束使用
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)
结束尝试
返回ColCombo
结束函数'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 =获取详细记录...
Application.DoEvents()
StrClosedYrs = BuildOpenOrClosedYrsStr(1)报名截止年$ b $乙dc_Category = GetComboBoxColumn_Category()
DataGridExpDet.Columns.Add(dc_Category)
私人小组DataGridExpDet_CellLeave(BYVAL发送者为对象,BYVAL E上System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridExpDet.CellLeave
。如果DataGridExpDet.CurrentCell.ColumnIndex = DataGridExpDet.Columns(量)。索引和不String.IsNullOrEmpty(DataGridExpDet(DataGridExpDet.CurrentCell.ColumnIndex,DataGridExpDet.CurrentCell.RowIndex).EditedFormattedValue)然后
如果DataGridExpDet(DataGridExpDet.CurrentCell.ColumnIndex,DataGridExpDet.CurrentCell.RowIndex).EditedFormattedValue< 0 Then
DataGridExpDet.Rows(DataGridExpDet.CurrentCell.RowIndex).DefaultCellStyle.ForeColor = Color.Gray
如果
结束如果
结束End Sub
任何帮助将非常感谢。
解决方案您有两个选项。
给出一列
Flat
style。这将改变ComboBox
的外观。但是如果你确定,那么这是容易的方法。只需将以下行添加到您的DataGridViewComboBoxColumn
创建:ColCombo.FlatStyle = FlatStyle.Flat
手动绘制
ComboBox
正确颜色的文本。要这样做,除了你的代码,处理DataGridView.CellPainting
和DataGridView.EditingControlShowing
事件。这个方法更长,但是如果你想保持ComboBox
的外观,那么这个选项应该很好地工作。'如果单元格是ComboBox单元格:抓取行ForeColor,
'像往常一样减去文本减去文本,然后手动
'正确的颜色。
'当单元格不处于编辑模式时,以正确的颜色显示单元格文本。
私人小组DataGridExpDet_CellPainting(发送者为对象,E作为DataGridViewCellPaintingEventArgs)处理DataGridExpDet.CellPainting
如果e.RowIndex> = 0和e.ColumnIndex> = 0,那么
。如果TypeOf运算DataGridExpDet.Rows (e.RowIndex).Cells(e.ColumnIndex)是的DataGridViewComboBoxCell然后
细胞暗淡作为的DataGridViewComboBoxCell = DataGridExpDet.Rows(e.RowIndex).Cells(e.ColumnIndex)
昏暗行作为的DataGridViewRow = DataGridExpDet。 Rows(e.RowIndex)
Dim color As Color = If(row.DefaultCellStyle.ForeColor.Name =0,color.Black,row.DefaultCellStyle.ForeColor)
使用brush =新的SolidBrush(颜色)
使用format =新的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)
'不要做以下一个 - 这就是我们正在覆盖。
'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 Sub
'显示时处理ComboBox控件的DrawItem事件。
'在编辑模式下,以正确的颜色绘制所选项目和项目选项的文本。
私人小组DataGridExpDet_EditingControlShowing(发送者为对象,E作为DataGridViewEditingControlShowingEventArgs)处理DataGridExpDet.EditingControlShowing
如果TypeOf运算e.Control是组合框然后
昏暗CB作为组合框= TryCast(e.Control,组合框)
cb.DrawMode = DrawMode.OwnerDrawFixed
RemoveHandler cb.DrawItem,AddressOf GridComboBox_DrawItem
AddHandler cb.DrawItem,AddressOf GridComboBox_DrawItem
End If
End Sub
'正常绘制背景和焦点,然后手动
'从
'对应的DisplayValue列的索引数据源行中绘制文本值。
Private Sub GridComboBox_DrawItem(sender As Object,e As DrawItemEventArgs)
e.DrawBackground()
e.DrawFocusRectangle()
Dim cb As ComboBox = TryCast ,ComboBox)
Dim dt As DataTable = TryCast(cb.DataSource,DataTable)
使用brush = New SolidBrush(cb.ForeColor)
e.Graphics.DrawString(dt。 Rows(e.Index).Item(sCategory),e.Font,brush,e.Bounds)
结束使用
End Sub
I have a grid with few columns to it. I have mentioned a condition as if Cell value of column Amount < 0 then change the text colour of a that row to Gray. Now one of my column has "DataGridViewComboBox" embedded to it. Now when the <0 condition met, text of all other columns from that row turns gray except this "DataGridViewComboBox" Column.
I would like to know how can I set the Combobox's selected text's colour to Gray?
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
Any help would be greatly appreciated.
解决方案You have two options. One is easy and the other is involved.
Give the column a
Flat
style. This will change the way theComboBox
looks. But if you're OK with that, then this is the easy approach. Simply add the following line to yourDataGridViewComboBoxColumn
creation:ColCombo.FlatStyle = FlatStyle.Flat
Manually draw the
ComboBox
text in the correct color. To do so, in addition to your code, handle theDataGridView.CellPainting
andDataGridView.EditingControlShowing
events. This method is longer, but if you want to keep the look of theComboBox
then this option should work nicely.' 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
这篇关于设置嵌入在datagridview单元格中的Combobox的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!