The best solution would be having a separate data layer, bind data source to your control using
binding and perform your search in data, not in control.
See, for example, this tutorial (CodeProject):
A Detailed Data Binding Tutorial[
^].
See also:
https://msdn.microsoft.com/en-us/library/System.Windows.Forms.DataGridView.DataSource%28v=VS.110%29.aspx[
^].
As to your code sample, it doesn't search anything, because you never traverse any set of cells. It become apparent to anyone who can notice that you never use any
for
or
foreach
loops. At the same time, you get some cells by hard-coded indices, which is not only bad for maintenance, but is simply unsafe. I think, there is nothing to discuss here. You have the collection properties like
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.cells%28v=vs.110%29.aspx[
^].
You can do some search in either row loop or two nested loops, to access all cells, or do something according to you storage semantics. But working with data layer is the most robust and decent approach.
—SA