Windows Develop Bookmark and Share   
 index > Windows Forms General > How to compare and hilight text in DataGridView?
 

How to compare and hilight text in DataGridView?

I have a datagridview with threecolumns, the first column is a checkbox, the second and third columns will have some similar text in them except for some changes. I want to compare and hilight the text in the second and third columns.

for examplethe second column will havetext: "wonder number 2332"

and the third column will have thetext: "wonder # 2332"

So in this example I want to hilight # 2332 with a different color. Is it possible?

Thanks for your help.

Neevarp  Thursday, March 22, 2007 8:11 PM

hi Neevarp:

you can use cellpaint event in datagridview and follow code works well in my pc

#123 can be red and aaa still black

void button2_Click(object sender, EventArgs e)

{

this.dataGridView2.Columns.Add(new DataGridViewTextBoxColumn());

this.dataGridView2[0, 0].Value = "aaa#123";

}

private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == 0 && e.ColumnIndex == 0)

{

int index = e.Value.ToString().IndexOf("#");

string black = e.Value.ToString().Substring(0, index);

string red = e.Value.ToString().Substring(index);

e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);

Font font = this.dataGridView1.Font;

Rectangle strRect = e.CellBounds;

strRect.Width = TextRenderer.MeasureText(black, font).Width;

e.Graphics.DrawString(black, font, Brushes.Black, strRect);

strRect.X = strRect.X+strRect.Width;

strRect.Width =TextRenderer.MeasureText(red, font).Width;

e.Graphics.DrawString(red, font, Brushes.Red, strRect);

e.Handled = true;

}

}

Bob zhu - SJTU  Friday, March 23, 2007 4:33 AM

hi Neevarp:

you can use cellpaint event in datagridview and follow code works well in my pc

#123 can be red and aaa still black

void button2_Click(object sender, EventArgs e)

{

this.dataGridView2.Columns.Add(new DataGridViewTextBoxColumn());

this.dataGridView2[0, 0].Value = "aaa#123";

}

private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.RowIndex == 0 && e.ColumnIndex == 0)

{

int index = e.Value.ToString().IndexOf("#");

string black = e.Value.ToString().Substring(0, index);

string red = e.Value.ToString().Substring(index);

e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);

Font font = this.dataGridView1.Font;

Rectangle strRect = e.CellBounds;

strRect.Width = TextRenderer.MeasureText(black, font).Width;

e.Graphics.DrawString(black, font, Brushes.Black, strRect);

strRect.X = strRect.X+strRect.Width;

strRect.Width =TextRenderer.MeasureText(red, font).Width;

e.Graphics.DrawString(red, font, Brushes.Red, strRect);

e.Handled = true;

}

}

Bob zhu - SJTU  Friday, March 23, 2007 4:33 AM
Thank you very much. that helped me...
Neevarp  Tuesday, March 27, 2007 9:38 PM

You can use google to search for other answers

Custom Search

More Threads

• Listview header
• TreeViews and scroll position
• Delete the Contents of a Directory?
• RichTextBox not adding lines of text quick enough.
• How to detect when ToolStripTextBox loses focus?
• Windows forms and Office Word
• bold and underline simultaneously(very very urgent)
• Avoid DLL Tempering
• How would you design this Windows form?
• Radio Button Grouping