c# - c# DataGridView DataGridViewTextBoxCell中的FindTextLocation或文本索引
问题描述
在我的应用程序中,我面临获取确切文本位置或索引的复杂性。我的文字会像
“AAAAAAA”+(……一个长空格……)+“BBBBBBBB”
我可以从我的 DatagridViewTextBoxCell 将这些值作为数组获取。但是为了突出显示的目的,我无法在牢房中获取他们的位置。请帮我解决这个问题。
我在 CellPainting 事件下获取值的代码是
DataGridViewTextBoxCell currentCell =
(DataGridViewTextBoxCell)grid.Rows[e.RowIndex].Cells[e.ColumnIndex]
提前致谢
解决方案
我不确定您的问题是如何搜索或如何绘制您的单元格。这是一个如何在字符串中搜索文本或使用正则表达式查找文本的示例。
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;
}
}
推荐阅读
- excel - 是否有类似于可与数组一起使用的 With 语句的东西?
- haskell - 斐波那契数列的 Haskell 尾递归
- python - 在 pytest 中为我的代码获取空白输出,不知道如何解决
- mysql - 为 pyspark 数据框中的每个键生成查询
- go - 什么决定了打包程序是否缓冲标准输出
- report - 有没有办法获取临时空手道报告文件夹的路径
- java - 如果在按下主页按钮后从后台调用 startActivity() 会静默失败
- sql - 在一系列 Union Alls 之后,对表的所有行计数一次
- html - HTML 表格卡住了
- json - 如何在 Go 中处理具有相同元素的嵌套结构