【问题标题】:c# FindTextLocation or Index of Text in DataGridView DataGridViewTextBoxCellc# FindTextLocation 或 DataGridView DataGridViewTextBoxCell 中的文本索引
【发布时间】:2019-11-21 07:12:13
【问题描述】:

在我的 Application 中,我面临着获取确切 Text Location 或 Index 的复杂性。我的文字会像

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

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

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

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

提前致谢

【问题讨论】:

  • 我不明白你的问题。你在寻找什么?您是否有包含输入文本和所需结果(索引或文本)的完整示例?
  • 基本上我想根据我通过文本框的搜索突出显示单元格文本。因此,如果单元格文本与我的搜索匹配,我想从单元格中获取文本位置。
  • 我可能会避免使用DataGridView 来突出显示搜索文本,而是依赖WebBrowser 控件。不确定它是否是您要查找的内容,但您可以在 this post 中找到搜索和突出显示数据中文本的工作示例。
  • @RezaAghaei,实现是针对winForms的,谢谢你的回复
  • 链接的实现也在 WinForms 中。它使用 T4 模板在运行时生成数据网格,并在 Form 中托管的 WebBrowser 控件中显示结果。另外,如果您需要在 html 内容和表单之间进行一些交互,您可以查看this post 从 Web 浏览器控件调用 C# 代码或从 C# 代码修改浏览器控件的内容。

标签: c# winforms datagridview


【解决方案1】:

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

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多