Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

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. Noe 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?
VB
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

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)
            .
            .
            .
            .
            .
            .
            .
            .
            .
        Catch ex As Exception
            ImpensaAlert(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

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.

Thanks!
Sachin
Posted
Updated 6-Aug-15 20:06pm
v2
Comments
sachinw 6-Aug-15 13:29pm    
Note that when the condition meet all the columns from that row turns gray except that "Category" column which has embedded dropdown to it.
Richard Deeming 6-Aug-15 14:24pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
sachinw 7-Aug-15 1:57am    
Yes, certainly. But, this is windows application and not web application so that isn't critical thing. Surely, I will work on this later.

Thanks for your suggestion.
sreeyush sudhakaran 9-Aug-15 3:29am    
You cannot change the text fore color of a DataGridviewComboBoxCell instead you can Draw text in the CellPainting event , Use a solid brush and draw your custom text on combobox cell. Another method is on combobox click your text can be changed.This is done by mounting a combobox to this cell on editing control showing event , in this case you can change fore color of combobox when it is showing on edit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900