首页 > 解决方案 > c# DataGridView DataGridViewTextBoxCell中的FindTextLocation或文本索引

问题描述

在我的应用程序中,我面临获取确切文本位置或索引的复杂性。我的文字会像

“AAAAAAA”+(……一个长空格……)+“BBBBBBBB”

我可以从我的 DatagridViewTextBoxCell 将这些值作为数组获取。但是为了突出显示的目的,我无法在牢房中获取他们的位置。请帮我解决这个问题。

我在 CellPainting 事件下获取值的代码是

DataGridViewTextBoxCell currentCell =
    (DataGridViewTextBoxCell)grid.Rows[e.RowIndex].Cells[e.ColumnIndex]

提前致谢

标签: c#winformsdatagridview

解决方案


我不确定您的问题是如何搜索或如何绘制您的单元格。这是一个如何在字符串中搜索文本或使用正则表达式查找文本的示例。

static void Main(string[] args)
{
    string someText = "AAAAAAAAAA sjkvsvkjq BBBBBBBBBBB";

    // This is how to find a Text within a String:
    string searchText = "BBBBBBBBBBB";
    int indexOfString = someText.IndexOf(searchText);
    Console.WriteLine("Text found at index " + indexOfString);

    // This is how to find text within a pattern:
    Regex regularExpression = new Regex("AAAAAAAAAA (.*) BBBBBBBBBBB");
    string textBetweenBraces = regularExpression.Match(someText).Groups[1].ToString();
    Console.WriteLine("Pattern matched. Text in Pattern: " + textBetweenBraces);

    // Paint your cell:
    if (regularExpression.IsMatch(someText))
    {
        DataGridViewTextBoxCell cell = null; // Select your cell
        cell.Style.BackColor = Color.Red;
    }
}

推荐阅读