0

In my Application , I am facing complication for fetching the exact Text Location or Index . My Text will be like

"AAAAAAAA" + (......a long space.......) + "BBBBBBBB"

I could able to get these values as array from my DatagridViewTextBoxCell . But i couldn't able to get their location in the cell for a highlight purpose. Please help me to solve this.

My Code to fetch value under CellPainting Event is

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

Thanks in advance

kara
  • 3,205
  • 4
  • 20
  • 34
  • I don't understand your question. What are you searching for? Do you have an complete sample with input text and the desired result (index or text)? – kara Nov 21 '19 at 07:37
  • Basically i want to highlight the Cell Text based on my search through a TextBox. so for that purpose i want to get the text- location from the cell if the cell text matches my search. – SiddarthVarunesh Nov 21 '19 at 07:43
  • I probably will avoid using `DataGridView` for this highlighting the search text and instead will rely on `WebBrowser` control. Not sure if it's what you are looking for, but you can find a working example of search and highlight text in data in [this post](https://stackoverflow.com/a/54675974/3110834). – Reza Aghaei Nov 21 '19 at 08:02
  • @RezaAghaei, the implementation is for winForms, thanks for your reply – SiddarthVarunesh Nov 21 '19 at 08:22
  • The linked implementation is also in WinForms. It's generating the grid of data at run-time using a T4 template and show the result in a `WebBrowser` control hosted in a `Form`. Also if you need some interaction between the html content and the form, you can take a look at [this post](https://stackoverflow.com/a/34840461/3110834) to call C# code from web browser control or modify the content of the browser control from C# code. – Reza Aghaei Nov 21 '19 at 08:31
  • I'd like to emphasis again, it's not a solution for what you are trying to implement in `DataGridView` and I'll not push you toward that direction. But it may be a good option for cases that you can easily implement a specific feature in html and you want somehow to integrate it with a Windows Forms application. – Reza Aghaei Nov 21 '19 at 08:36

1 Answers1

0

I'm not sure if your problem is how to search or how to paint your cell. Here an example how to search for a text within a string or use a regular expression to find a text.

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;
    }
}
kara
  • 3,205
  • 4
  • 20
  • 34