Change the text color in a datagridviewtextboxcell

Posted on 2009-04-15
Last Modified: 2012-05-06
How can I change the color of the text in a cell according to it's position within the cell?  In other words, I want to have the first four characters be light blue and the last four to be light green, all within the same cell.  I've tried to split the text, which is easy, and then use graphics to drawstring with different colored brushes, but it doesn't seem to work.  Any ideas?
Question by:ddepuemd
    LVL 4

    Accepted Solution

    You have to override the datagridViewTextBoxCell Paint method. I never tried by myself but there is someone tried here in this link with the full code; I analyzed and seems logical.

    Hope this information will help you.
    LVL 53

    Assisted Solution

    I'm not sure if these can help you further in your research...

    Datagridview multi color cell text

    Changing text color in a DataGrid using itemRenderers

    Author Comment

    I've tried all of those suggestions with no success at all.

    Author Comment

    I found a way to do this in the paint method of the grid:

                    If e.ColumnIndex = 10 Then
                        If Not IsDBNull(dgvMainGrid.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then
                            Dim value As Integer = Val(dgvMainGrid.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                            If value < 0 Then
                                dgvMainGrid.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Red
                                dgvMainGrid.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.ForeColor = Color.White
                            End If
                        End If
                    End If

    Thanks for the help!

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now