Windows Develop Bookmark and Share   
 index > Windows Forms General > Color text in cell of datagrid
 

Color text in cell of datagrid

Hi,

I want to change only a color of part of the text in the cell of datagrid.

For example I have in cell "1(5)" and want to see "1(5)".

I am using .Net 1.1 and working with VS2003.

How can I do this?

Please give me an example.

Thank's

Alexei

Alexei_shk  Monday, August 13, 2007 8:33 AM

Hi,

Here is a sample code. You can try by entering something including "-" in the cell.

example , enter "Color-Test". Then my code will color the string "color" to red and "-Test" to violet.

This is just an example and easy way. You can also create your owm custom cell with your formatting.

Here is code:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.FormattedValue != null)

{

string cellvalue = e.FormattedValue.ToString();

if (cellvalue.Contains("-"))

{

string redvalue = cellvalue.Split('-')[0];

string yellowvalue = "-" + cellvalue.Split('-')[1];

DataGridViewCell cell = (DataGridViewCell)dataGridView1[e.ColumnIndex, e.RowIndex];

if (cell != null)

{

e.PaintBackground(e.ClipBounds, true);

e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);

SizeF redsize = e.Graphics.MeasureString(redvalue, dataGridView1.Font);

Brush redbrush = Brushes.Red;

Brush violetbrush = Brushes.Violet;

e.Graphics.DrawString(redvalue, dataGridView1.Font, redbrush, new PointF(e.CellBounds.X, e.CellBounds.Y));

e.Graphics.DrawString(yellowvalue, dataGridView1.Font, violetbrush, new PointF(e.CellBounds.X + redsize.Width + 1, e.CellBounds.Y));

e.Handled = true;

}

}

}

}

*Don't forget to attach cell_painting to your datagridview.

wirol  Monday, August 13, 2007 9:13 AM

Hi,

Here is a sample code. You can try by entering something including "-" in the cell.

example , enter "Color-Test". Then my code will color the string "color" to red and "-Test" to violet.

This is just an example and easy way. You can also create your owm custom cell with your formatting.

Here is code:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.FormattedValue != null)

{

string cellvalue = e.FormattedValue.ToString();

if (cellvalue.Contains("-"))

{

string redvalue = cellvalue.Split('-')[0];

string yellowvalue = "-" + cellvalue.Split('-')[1];

DataGridViewCell cell = (DataGridViewCell)dataGridView1[e.ColumnIndex, e.RowIndex];

if (cell != null)

{

e.PaintBackground(e.ClipBounds, true);

e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);

SizeF redsize = e.Graphics.MeasureString(redvalue, dataGridView1.Font);

Brush redbrush = Brushes.Red;

Brush violetbrush = Brushes.Violet;

e.Graphics.DrawString(redvalue, dataGridView1.Font, redbrush, new PointF(e.CellBounds.X, e.CellBounds.Y));

e.Graphics.DrawString(yellowvalue, dataGridView1.Font, violetbrush, new PointF(e.CellBounds.X + redsize.Width + 1, e.CellBounds.Y));

e.Handled = true;

}

}

}

}

*Don't forget to attach cell_painting to your datagridview.

wirol  Monday, August 13, 2007 9:13 AM

You can use google to search for other answers

Custom Search

More Threads

• passing data between winforms
• ACCESS Form/Field Representation
• serialize TreeNode-derived classes
• Datagridview cell backcolor
• Menu in .Net Windows Forms
• Difference between User Controls and class inherited from a control
• inserting data in table from richtextbox
• a problem with threads
• Control property bags and assigning event listeners dynamically
• Set question