Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:11:19+00:00 2026-06-09T05:11:19+00:00

In VB .NET I have 3 characters which are added to a DataGridView cell

  • 0

In VB .NET I have 3 characters which are added to a DataGridView cell depending on some calculations.

They are rank change arrows and work fine, but I want the up arrow to be green and the down arrow to be red.

Dim strup As String = "▲"
Dim strdown As String = "▼"
Dim strsame As String = "▬"

So in the cell a change of negative three will look like ▼3 and plus 3 will look like ▲3 where the text and symbol are different colors.

How can I change the color of the first character in DataGridView cell?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:11:20+00:00Added an answer on June 9, 2026 at 5:11 am

    There is no easy way to do this if you have anything more than just the character in question in the cell (you would need to do some form of custom painting).

    If you only have those characters then this is very easy with the CellFormatting event:

    void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        e.CellStyle.Font = new Font("Arial Unicode MS", 12);
        if (dataGridView1.Columns[e.ColumnIndex].Name == "CorrectColumnName")
        {
            if (e.Value == "▲")
                e.CellStyle.ForeColor = Color.Green;
            else if (e.Value == "▼")
                e.CellStyle.ForeColor = Color.Red;
            else
                e.CellStyle.ForeColor = Color.Black;
        }
    }
    

    If you do want different colors within the same cell then something like the following code is required (this handles the CellPainting event):

    void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex == -1 || e.RowIndex == -1)
            return;
    
        if (dataGridView1.Columns[e.ColumnIndex].Name == "CorrectColumnName")
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
    
            if (e.FormattedValue.ToString().StartsWith("▲", StringComparison.InvariantCulture))
            {
                RenderCellText(Color.Green, e);
            }
            else if (e.FormattedValue == "▼")
            {
                RenderCellText(Color.Red, e);
            }
            else
                RenderCellText(SystemColors.WindowText, e);
    
            e.Handled = true;
        }
    }
    
    private void RenderCellText(Color color, DataGridViewCellPaintingEventArgs e)
    {
        string text = e.FormattedValue.ToString();
        string beginning = text.Substring(0, 1);
        string end = text.Substring(1);
        Point topLeft = new Point(e.CellBounds.X, e.CellBounds.Y + (e.CellBounds.Height / 4));
    
        TextRenderer.DrawText(e.Graphics, beginning, this.dataGridView1.Font, topLeft, color);
        Size s = TextRenderer.MeasureText(beginning, this.dataGridView1.Font);
    
        Point p = new Point(topLeft.X + s.Width, topLeft.Y);
        TextRenderer.DrawText(e.Graphics, end, this.dataGridView1.Font, p, SystemColors.WindowText);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one asp.net application, which has some problems while i am entering the
I have a string which at some point contains the set of characters in
I am writing a library in VB.NET in which I have added, among others,
I have gone through this page... http://www.php.net/manual/en/function.hash.php MD5 is 32 characters long while sha1
Does .NET have some type of built in function that allows filtering on Gridviews?
Does .NET have anything similar to Perl arrays, which are indexed numerically but automatically
I have a datagrid in C#.net I have made some columns in grid editable.
We are using ASP.NET MVC with LINQ to SQL. We added some features and
Using VB.NET - I have a string: ##RES00012##Some value ##RES00034##Another value That I want
I have converted this existing C# .NET 2.0 code (which appears to take in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.