具有WrapMode的TextCell上DataGridView中的TextHighlight [英] TextHighlight in DataGridView on TextCell with WrapMode
问题描述
在WinForm上的DataGridView中,我有:
in a DataGridView on WinForm i have :
dataGridViewAnzeige.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
dataGridViewAnzeige.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
要绘制背景,我要在CellPainting上绘制它,我的搜索执行拆分搜索"过滤更多结果
to paint the background i do it at the CellPainting, my search doing a "split search" to filter more results
但是包装文字的测量失败了,有什么主意吗?
but the measuring of the Wraped Text is faild, any ideas ?
第二行的位置失败..
the position on the second line fails ..
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1 && dataGridView1.Columns[e.ColumnIndex].Name != "Id")
{
var suchtext = searchStringTextBox.Split(null);
List<Rectangle> rectangleList = new List<Rectangle>();
// Check data for search
if (!String.IsNullOrWhiteSpace(searchStringTextBox.Trim()))
{
foreach (var txtSuche in suchtext)
{
String gridCellValue = e.FormattedValue.ToString();
// check the index of search text into grid cell.
int startIndexInCellValue = gridCellValue.ToLower().IndexOf(txtSuche.Trim().ToLower());
// IF search text is exists inside grid cell then startIndexInCellValue value will be greater then 0 or equal to 0
if (startIndexInCellValue >= 0)
{
e.Handled = true;
e.PaintBackground(e.CellBounds, true);
//the highlite rectangle
Rectangle hl_rect = new Rectangle();
hl_rect.Y = e.CellBounds.Y + 2;
//find the size of the text before the search word in grid cell data.
String sBeforeSearchword = gridCellValue.Substring(0, startIndexInCellValue);
//size of the search word in the grid cell data
String sSearchWord = gridCellValue.Substring(startIndexInCellValue, txtSuche.Trim().Length);
Size s1 = TextRenderer.MeasureText(e.Graphics, sBeforeSearchword, e.CellStyle.Font, e.CellBounds.Size, TextFormatFlags.TextBoxControl);
Size s2 = TextRenderer.MeasureText(e.Graphics, sSearchWord, e.CellStyle.Font, e.CellBounds.Size, TextFormatFlags.TextBoxControl);
if (s1.Width > 5)
{
hl_rect.X = e.CellBounds.X + s1.Width - 5;
hl_rect.Width = s2.Width - 6;
}
else
{
hl_rect.X = e.CellBounds.X + 2;
hl_rect.Width = s2.Width - 6;
}
//fail on textalign in middle
hl_rect.Height = s2.Height;
rectangleList.Add(hl_rect);
}
}
}
//color for showing highlighted text in grid cell
SolidBrush hl_brush;
hl_brush = new SolidBrush(Color.Yellow);
//paint the background behind the search word
foreach(var recto in rectangleList)
{
e.Graphics.FillRectangle(hl_brush, recto);
}
hl_brush.Dispose();
e.PaintContent(e.CellBounds);
}
}
要绘制背景,我会在CellPainting上进行绘制,我的搜索会进行拆分搜索",过滤更多结果
to paint the background i do it at the CellPainting, my search doing a "split search" to filter more results
但是包装文字的测量失败了,有什么主意吗?
but the measuring of the Wraped Text is faild, any ideas ?
第二行的位置失败..
the position on the second line fails ..
推荐答案
我有解决方案,请更正或显示更好的解决方案
I've got a solution, please correct or show the better solution
if (s1.Width > 5)
{
hl_rect.X = e.CellBounds.X + s1.Width - 5;
hl_rect.Width = s2.Width - 6;
if (s1.Height > s2.Height)
{
String breakWord = gridCellValue.Substring(indexOfN, startIndexInCellSecondeRowValue);
Size s3 = TextRenderer.MeasureText(e.Graphics, breakWord, e.CellStyle.Font, e.CellBounds.Size, TextFormatFlags.TextBoxControl);
hl_rect.X = e.CellBounds.X + 2 + s3.Width -7;
hl_rect.Y = e.CellBounds.Y + s2.Height +2;
}
}
else
{
hl_rect.X = e.CellBounds.X + 2;
hl_rect.Width = s2.Width - 6;
}
在我的情况下有效...
it works in my case...
这篇关于具有WrapMode的TextCell上DataGridView中的TextHighlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!