Multi-color text in single cell of datagridview in C#
-
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
-
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
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);
-
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);
Mind blowing..... thanks a lot .....