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()
{
}
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);
}
public StringPrintInfo[] GetConsectiveNumbers(int numberConsecutive, String input, Brush bOriginal, Brush bConsecutive)
{
List<StringPrintInfo> formatInformation = new List<StringPrintInfo>();
int inputCount = 0;
StringPrintInfo tmpStrPrntInfo;
String regExp = "\\b[0-9]{4}\\b";
Regex exp = new Regex(regExp);
foreach (Match match in exp.Matches(input))
{
if (inputCount < match.Index)
{
tmpStrPrntInfo = new StringPrintInfo();
String strTmp = input.Substring(0, match.Index);
tmpStrPrntInfo.str = strTmp;
tmpStrPrntInfo.brush = bOriginal;
inputCount = match.Index;
formatInformation.Add(tmpStrPrntInfo);
}
tmpStrPrntInfo = new StringPrintInfo();
tmpStrPrntInfo.str = match.Value;
tmpStrPrntInfo.brush = bConsecutive;
inputCount = match.Index + match.Length;
formatInformation.Add(tmpStrPrntInfo);
}
if (inputCount < input.Length)
{
tmpStrPrntInfo = new StringPrintInfo();
String strTmp = input.Substring(inputCount);
tmpStrPrntInfo.str = strTmp;
tmpStrPrntInfo.brush = bOriginal;
inputCount = input.Length;
formatInformation.Add(tmpStrPrntInfo);
}
return formatInformation.ToArray();
}
public void PrintConsectiveNumbers(StringPrintInfo[] toPrint,Graphics graphics, Font font ,Brush textBrush,Rectangle cellBounds, StringFormat stringFormat)
{
float xOffset = 0;
for(int i = 0;i<toPrint.Length;i++)
{
if (i > 0)
{
SizeF stringSize = graphics.MeasureString(toPrint[i-1].str, font);
xOffset += stringSize.Width;
}
if (cellBounds.X+ xOffset + graphics.MeasureString(toPrint[i].str, font).Width > cellBounds.X + cellBounds.Width)
{
string cutDown = toPrint[i].str;
for (int j = cutDown.Length; j > 0; j--)
{
cutDown = cutDown.Substring(0, j);
if (cellBounds.X + xOffset + graphics.MeasureString(cutDown, font).Width <= cellBounds.X + cellBounds.Width)
{
graphics.DrawString(cutDown, font, toPrint[i].brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat);
break;
}
}
}
else
{
graphics.DrawString(toPrint[i].str, font, toPrint[i].brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat);
}
}
}
public struct StringPrintInfo
{
public String str;
public Brush brush;
}
}
DataSet dataSet = new DataSet();
DataTable dStable = new DataTable();
DataColumn dSCol1 = new DataColumn("name", typeof(string));
DataColumn dSCol2 = new DataColumn("value", typeof(string));
DataRow rTemp;
DataGridViewColumnCollection dGVColumnCollection;
DataGridViewColumn dGVColumn1 = new DataGridViewColumn();
DataGridViewColumn dGVColumn2 = new DataGridViewColumn();
public partial class Form1 : Form
{
DataSet dataSet = new DataSet();
DataTable dStable = new DataTable();
DataColumn dSCol1 = new DataColumn("name", typeof(string));
DataColumn dSCol2 = new DataColumn("value", typeof(string));
DataRow rTemp;
DataGridViewColumnCollection dGVColumnCollection;
DataGridViewColumn dGVColumn1 = new DataGridViewColumn();
DataGridViewColumn dGVColumn2 = new DataGridViewColumn();
public Form1()
{
InitializeComponent();
dStable.TableName = "sample";
dStable.Columns.Add(dSCol1);
dStable.Columns.Add(dSCol2);
rTemp = dStable.NewRow();
rTemp.ItemArray = new object[]{"bill", " 000 0000 0000000 0000000 0000000000"};
dStable.Rows.Add(rTemp);
rTemp = dStable.NewRow();
rTemp.ItemArray = new object[]{"ted", " 111 1111 1111111 1111111 1111111111"};
dStable.Rows.Add(rTemp);
rTemp = dStable.NewRow();
rTemp.ItemArray = new object[] { "john", " 222 2222 2222222 2222222 2222222222" };
dStable.Rows.Add(rTemp);
dataSet.Tables.Add(dStable);
dGVColumnCollection = dataGridView1.Columns;
dGVColumnCollection.Add("col1", "Name");
dGVColumnCollection.Add("col2", "Value");
dGVColumn1 = dGVColumnCollection[0];
dGVColumn2 = dGVColumnCollection[1];
foreach(DataRow row in dataSet.Tables[0].Rows)
{
DataGridViewRow dGVRow = new DataGridViewRow();
DataGridViewTextBoxCell textCell = new DataGridViewTextBoxCell();
MultiColourDataGridViewTextBoxCell textCel2 = new MultiColourDataGridViewTextBoxCell();
textCell.Value = row.ItemArray[0];
textCel2.Value = row.ItemArray[1];
dGVRow.Cells.Add(textCell);
dGVRow.Cells.Add(textCel2);
dataGridView1.Rows.Add(dGVRow);
}
}
}
http://img2.pict.com/b4/07/ad/2401377/0/gridcellcolour1.jpg
http://img2.pict.com/91/13/8d/2401376/0/gridcellcolour2.jpg
modified on Tuesday, January 5, 2010 9:17 PM
|