[英]Set the text color of Combobox embedded in datagridview cell
我有一個很少有列的網格。 我已經提到過一個條件,好像“列Amount”的“單元格”值<0,然后將該行的文本顏色更改為“灰色”。 現在,我的專欄中之一已嵌入“ DataGridViewComboBox”。 現在,當<0條件滿足時,該行的所有其他列的文本都會變為灰色,但此“ DataGridViewComboBox”列除外。
我想知道如何將組合框的選定文本的顏色設置為灰色?
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
任何幫助將不勝感激。
通常,要更改文本的顏色,請更改.forecolor
ComboBox1.ForeColor = Color.Gray
將更改組合框中所有文本的顏色。
但是,我不認為默認情況下datagridview組合框具有此選項。
每當我做這樣的自定義工作時,我通常都會使用RadComboBox 。 他們有大量的自定義選項,可讓您完成所需的任何事情。
編輯
啊哈,找到了! -你去
顯然,您像普通的組合框一樣進行編輯。
您有兩個選擇。 一個很簡單,另一個很簡單。
給列設置一個Flat
樣式。 這將改變ComboBox
外觀。 但是,如果您對此表示滿意,那么這是簡單的方法。 只需將以下行添加到您的DataGridViewComboBoxColumn
創建中:
ColCombo.FlatStyle = FlatStyle.Flat
手動以正確的顏色繪制ComboBox
文本。 為此,除了您的代碼外,還要處理DataGridView.CellPainting
和DataGridView.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.