在单个DataGridView单元格中设置两种颜色的文本 [英] Set two colors text in a single DataGridView Cell
问题描述
我需要更改单元格中最后5个字符的颜色,但我不知道该怎么做。
我在CellPainting事件中有这个代码但是不工作:
private void dgvSorteados_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
{
int sector = 0;
int.TryParse(dgvSorteados.Rows [e.RowIndex] .Cells [0] .Value.ToString(),out sector);
if(sector == 3&& rdbSenete3.Checked)
{
if(dgvSorteados.Columns [1] .Index == e.ColumnIndex&& e.RowIndex> ; = 0)
{
string bolillas =(String)e.Value;
string [] bolillasArray = bolillas.Split(' - ');
string bolillasMin = string.Join( - ,bolillasArray.Take(12));
string bolillasResto = string.Join( - ,bolillasArray.Skip(12));
使用(Brush gridBrush = new SolidBrush(dgvSorteados.GridColor),backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
//擦除单元格。
e.Graphics.FillRectangle(backColorBrush,eCellBounds);
//绘制单元格的文本内容,忽略对齐方式。
e.Graphics.DrawString((String)bolillasMin,e.CellStyle.Font,Brushes.Black,e.CellBounds.X + 2,e.CellBounds.Y + 2,StringFormat.GenericDefault);
if(!string.IsNullOrEmpty(bolillasResto))
{
e.Graphics.DrawString( - +(String)bolillasResto,e.CellStyle.Font,Brushes.Crimson, CellBounds.X + 2 + bolillasMin.Length,e.CellBounds.Y + 2,StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}
代码显示没有行的DataGridView。
您可以使用 e避免背景绘画代码。 PaintBackground
调用。此外,只有当 ContentForeGround
正在绘制时,您必须绘制字符串。使用 e.PaintParts
来识别绘画操作。看我的示例代码的用法。它需要调整,但你会得到一个想法。
示例代码:
void dataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
{
if(e.RowIndex!= -1&& e.Value!= null&& e.Value.ToString ).Length> 5&& eColumnIndex == InterestedColumnIndex)
{
if(!e.Handled)
{
e.Handled = true;
e.PaintBackground(e.CellBounds,dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex] .Selected);
}
如果((e.PaintParts& DataGridViewPaintParts.ContentForeground)!= DataGridViewPaintParts.None)
{
string text = e.Value.ToString();
string textPart1 = text.Substring(0,text.Length - 5);
string textPart2 = text.Substring(text.Length - 5,5);
Size fullsize = TextRenderer.MeasureText(text,e.CellStyle.Font);
尺寸size1 = TextRenderer.MeasureText(textPart1,e.CellStyle.Font);
尺寸size2 = TextRenderer.MeasureText(textPart2,e.CellStyle.Font);
Rectangle rect1 = new Rectangle(e.CellBounds.Location,e.CellBounds.Size);
using(Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(textPart1,e.CellStyle.Font,cellForeBrush,rect1);
}
rect1.X + =(fullsize.Width - size2.Width);
rect1.Width = e.CellBounds.Width;
e.Graphics.DrawString(textPart2,e.CellStyle.Font,Brushes.Crimson,rect1);
}
}
}
I have a data grid view in my C# Windows Application. I need to change the color of the last 5 characters in a cell but i dont know how to do it.
I have this code in the CellPainting event but is not working:
private void dgvSorteados_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sector = 0;
int.TryParse(dgvSorteados.Rows[e.RowIndex].Cells[0].Value.ToString(), out sector);
if (sector == 3 && rdbSenete3.Checked)
{
if (dgvSorteados.Columns[1].Index == e.ColumnIndex && e.RowIndex >= 0)
{
string bolillas = (String)e.Value;
string[] bolillasArray = bolillas.Split('-');
string bolillasMin = string.Join("-", bolillasArray.Take(12));
string bolillasResto = string.Join("-", bolillasArray.Skip(12));
using (Brush gridBrush = new SolidBrush(dgvSorteados.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the text content of the cell, ignoring alignment.
e.Graphics.DrawString((String)bolillasMin, e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault);
if (!string.IsNullOrEmpty(bolillasResto))
{
e.Graphics.DrawString("-" + (String)bolillasResto, e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2 + bolillasMin.Length, e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}
This code shows the DataGridView without rows.
You can avoid the background painting code by using the e.PaintBackground
call. Also you have to draw the strings only when the ContentForeGround
is being painted. Use the e.PaintParts
to identify the painting operation. See my sample code for its usage. It needs tweaking but you'll get an idea.
Sample Code:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex != -1 && e.Value != null && e.Value.ToString().Length > 5 && e.ColumnIndex == InterestedColumnIndex)
{
if (!e.Handled)
{
e.Handled = true;
e.PaintBackground(e.CellBounds, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected);
}
if ((e.PaintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None)
{
string text = e.Value.ToString();
string textPart1 = text.Substring(0, text.Length - 5);
string textPart2 = text.Substring(text.Length - 5, 5);
Size fullsize = TextRenderer.MeasureText(text,e.CellStyle.Font);
Size size1 = TextRenderer.MeasureText(textPart1, e.CellStyle.Font);
Size size2 = TextRenderer.MeasureText(textPart2, e.CellStyle.Font);
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
using (Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(textPart1, e.CellStyle.Font, cellForeBrush, rect1);
}
rect1.X += (fullsize.Width - size2.Width);
rect1.Width = e.CellBounds.Width;
e.Graphics.DrawString(textPart2, e.CellStyle.Font, Brushes.Crimson, rect1);
}
}
}
这篇关于在单个DataGridView单元格中设置两种颜色的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!