سلام
دوستان، اگر در دیتاگرید ویوی خودتون دو ستون داشته باشید که ستون صفرم، حاوی رشته های انگلیسی و ستون یکم، حاوی رشته های فارسی باشه، می تونید توسط این کد، رشته ای رو که کاربر در textbox مورد جستجو قرار می ده رو در دیتاگرید ویو رنگی کنید.
نکته ای که هست اینه که برای رشته های فارسی، محدودیتی وجود دارد، مثلاً:
ب خ ار در خروجی می آید
به جای
بخار
که ظاهراً در این محیط نمی شه حلش کرد چون پیچیده است.
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex==0 && e.RowIndex >= 0)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
if (e.RowIndex != -1 && e.Value != null && !string.IsNullOrEmpty(textBox1.Text))
{
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 = "", textPart2 = "", textPart3 = "";
int Index = text.IndexOf(textBox1.Text);
int length = textBox1.Text.Length;
if (Index == 0)
{
textPart1 = text.Substring(0, length);
textPart2 = text.Substring(length);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
if (Index + length < text.Length)
{
textPart3 = text.Substring(Index + length);
}
}
else if (Index == (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
}
else
{
textPart1 = textPart2 = textPart3 = text;
}
TextFormatFlags format = TextFormatFlags.Left | TextFormatFlags.VerticalCenter| TextFormatFlags.NoPadding;
Size size2 = TextRenderer.MeasureText(e.Graphics, textPart2, e.CellStyle.Font, Size.Empty, format);
Size size1 = TextRenderer.MeasureText(e.Graphics, textPart1, e.CellStyle.Font, Size.Empty, format);
Size size3 = TextRenderer.MeasureText(e.Graphics, textPart3, e.CellStyle.Font, Size.Empty, format);
if (Index == 0)
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
rect1.X += size1.Width;
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X += size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
if (Index + length < text.Length)
{
rect1.X += size1.Width;
TextRenderer.DrawText(e.Graphics, textPart3, e.CellStyle.Font, rect1, Color.Black, format);
}
}
else if (Index == (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X += size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
}
else
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, text, e.CellStyle.Font, rect1, Color.Black, format);
}
}
}
}
}
}
else if (e.ColumnIndex == 1 && e.RowIndex >= 0)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
if (e.RowIndex != -1 && e.Value != null && !string.IsNullOrEmpty(textBox1.Text))
{
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 = "", textPart2 = "", textPart3 = "";
int Index = text.IndexOf(textBox1.Text);
int length = textBox1.Text.Length;
if (Index == 0)
{
textPart1 = text.Substring(0, length);
textPart2 = text.Substring(length);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
if (Index + length < text.Length)
{
textPart3 = text.Substring(Index + length);
}
}
else if (Index == (text.Length - 1))
{
textPart2 = text.Substring(0, Index);
textPart1 = text.Substring(Index, length);
}
else
{
textPart1 = textPart2 = textPart3 = text;
}
TextFormatFlags format = TextFormatFlags.Right | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding | TextFormatFlags.RightToLeft;
Size size2 = TextRenderer.MeasureText(e.Graphics, textPart2, e.CellStyle.Font, Size.Empty, format);
Size size1 = TextRenderer.MeasureText(e.Graphics, textPart1, e.CellStyle.Font, Size.Empty, format);
Size size3 = TextRenderer.MeasureText(e.Graphics, textPart3, e.CellStyle.Font, Size.Empty, format);
int totalWidth = size2.Width + size1.Width + size3.Width;
if (Index == 0)
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
rect1.X -= size1.Width;
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
}
else if (0 < Index && Index < text.Length && Index != (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X -= size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
if (Index + length < text.Length)
{
rect1.X -= size1.Width;
TextRenderer.DrawText(e.Graphics, textPart3, e.CellStyle.Font, rect1, Color.Black, format);
}
}
else if (Index == (text.Length - 1))
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, textPart2, e.CellStyle.Font, rect1, Color.Black, format);
rect1.X -= size2.Width;
TextRenderer.DrawText(e.Graphics, textPart1, e.CellStyle.Font, rect1, Color.Red, format);
}
else
{
Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
TextRenderer.DrawText(e.Graphics, text, e.CellStyle.Font, rect1, Color.Black, format);
}
}
}
}
}
}