4
Answers

Thread searching a DataGridView Control

Joe Pool

Joe Pool

14y
3.5k
1
As my people are entering data into a textbox, I want to bring the most relevant row of data into view on my DataGridView control.

I do this now, but I can't do it in a thread since it is part of the Form and not thread safe.

How can I search a DataGridView's data from within a thread?

Example:
int FindRow(string textBoxValue) {
  for (int i = 0; i < DataGridView1.Rows.Count; i++) {
    if (DataGridView1.Rows[i].Cells[x].Value.ToString() == textBoxValue) {
      return DataGridView1.Rows[i].Index;
    }
  }
  return 0;
}

The above is not thread safe, of course.

I don't want to pass the entire DataGridView object to a thread as a parameter because it can be rather large and our clerks enter data pretty fast!
Answers (4)
1
Nitin Sontakke

Nitin Sontakke

NA 11.7k 2.2k 8y
You have used int.TryParse, similarly just use double.TryParse. That's all you have to do.
Accepted
2
ketan borsdiya

ketan borsdiya

NA 1.5k 3k 8y
Hi Raja,
Use below code snippet for your problem,
var regex = new Regex(@"^[0-9]*(?:\.[0-9]*)?$");
if (regex.IsMatch(ds.Tables[s].Rows[i][Columncountvalue + 10].ToString().Trim()))
{
//statement
}
0
Raja

Raja

NA 1.7k 45.2k 8y
Thank you so much