Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multi-color text in single cell of datagridview in C#

Multi-color text in single cell of datagridview in C#

Scheduled Pinned Locked Moved C#
csharpjsontutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    auaryamane007
    wrote on last edited by
    #1

    Hi All, Can anyone tell me how can I have a multicolor string in a single cell of a datagridview. For example " 000 0000 0000000 0000000 0000000000", now I want the four consecutive zeros "0000" in red and rest in black. Is it possible to do that. Thanks and Regards Akshay

    P 1 Reply Last reply
    0
    • A auaryamane007

      Hi All, Can anyone tell me how can I have a multicolor string in a single cell of a datagridview. For example " 000 0000 0000000 0000000 0000000000", now I want the four consecutive zeros "0000" in red and rest in black. Is it possible to do that. Thanks and Regards Akshay

      P Offline
      P Offline
      petercrab
      wrote on last edited by
      #2

      Hi, You need to override the paint method for the cell object. Cells that contain just text are instances of DataGridViewTextBoxCell Class, so override that. Below is my attempt at a new class with over-ridden paint method. The draw method also only draws what will fit in cell so there will be no overlapping of data between cells. I have tried to make it as reusable as possible so you can define what colour you wish to change to and the number of consecutive numbers that should have colour changed. I also include code I used to test the class out and a couple of screenshots showing it working.

      public class MultiColourDataGridViewTextBoxCell : DataGridViewTextBoxCell
      {
      public MultiColourDataGridViewTextBoxCell():base()
      {
      }

          /// <summary>
          /// overide paint method to colour text dependant on number of consecutive numbers
          /// </summary>
          /// <param name="graphics"></param>
          /// <param name="clipBounds"></param>
          /// <param name="cellBounds"></param>
          /// <param name="rowIndex"></param>
          /// <param name="cellState"></param>
          /// <param name="value"></param>
          /// <param name="formattedValue"></param>
          /// <param name="errorText"></param>
          /// <param name="cellStyle"></param>
          /// <param name="advancedBorderStyle"></param>
          /// <param name="paintParts"></param>
          protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
          {
              int fontSize = 9;
              Brush textBrush = Brushes.Green;
              Font font = new Font(FontFamily.GenericSansSerif,fontSize,FontStyle.Regular);
              String stringPrint = (String)formattedValue;
      
              graphics.FillRectangles(Brushes.White,new Rectangle\[\]{cellBounds});
              StringPrintInfo\[\] stringPrintInfo = GetConsectiveNumbers(4, stringPrint, Brushes.Black, Brushes.Red);
              PrintConsectiveNumbers(stringPrintInfo,graphics, font,textBrush,cellBounds,StringFormat.GenericDefault);
              this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
      
      A 1 Reply Last reply
      0
      • P petercrab

        Hi, You need to override the paint method for the cell object. Cells that contain just text are instances of DataGridViewTextBoxCell Class, so override that. Below is my attempt at a new class with over-ridden paint method. The draw method also only draws what will fit in cell so there will be no overlapping of data between cells. I have tried to make it as reusable as possible so you can define what colour you wish to change to and the number of consecutive numbers that should have colour changed. I also include code I used to test the class out and a couple of screenshots showing it working.

        public class MultiColourDataGridViewTextBoxCell : DataGridViewTextBoxCell
        {
        public MultiColourDataGridViewTextBoxCell():base()
        {
        }

            /// <summary>
            /// overide paint method to colour text dependant on number of consecutive numbers
            /// </summary>
            /// <param name="graphics"></param>
            /// <param name="clipBounds"></param>
            /// <param name="cellBounds"></param>
            /// <param name="rowIndex"></param>
            /// <param name="cellState"></param>
            /// <param name="value"></param>
            /// <param name="formattedValue"></param>
            /// <param name="errorText"></param>
            /// <param name="cellStyle"></param>
            /// <param name="advancedBorderStyle"></param>
            /// <param name="paintParts"></param>
            protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
            {
                int fontSize = 9;
                Brush textBrush = Brushes.Green;
                Font font = new Font(FontFamily.GenericSansSerif,fontSize,FontStyle.Regular);
                String stringPrint = (String)formattedValue;
        
                graphics.FillRectangles(Brushes.White,new Rectangle\[\]{cellBounds});
                StringPrintInfo\[\] stringPrintInfo = GetConsectiveNumbers(4, stringPrint, Brushes.Black, Brushes.Red);
                PrintConsectiveNumbers(stringPrintInfo,graphics, font,textBrush,cellBounds,StringFormat.GenericDefault);
                this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
        
        A Offline
        A Offline
        auaryamane007
        wrote on last edited by
        #3

        Mind blowing..... thanks a lot .....

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups