Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
 
I'm trying to find a string in all my datagridview.
To do it, I can creat a string buffer of all its values iterating cell per cell.
But I wonder how could I use the "Contain" function with a string as argument.
 
Thank you and sorry for my rudimentary english haha
Posted 7-Jun-12 12:06pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If your purpose is to find the Cell in the DataGridView containing a given Text, then I think it is better to use the Rows and Cells collection of DataGridView to generate a List of DataGridViewCell which contain the given text. Then on a button click set the CurrentCell of DataGridView to the found cells as shown below:
//Declare a List at Class level to hold the containing cells
//and an index to hold the current containing cell
List<datagridviewcell> containingCells = new List<datagridviewcell>();
int currentContainingCellListIndex = 0;
//In Event handler of Find button iterate all the rows and cells in 
//each row to find the cells containing the required text
//if found add that to the containingCells List   
ButtonFindText.Click += (sender, args) => {
    containingCells.Clear();
    currentContainingCellListIndex=0;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    	foreach (DataGridViewCell cell in row.Cells)
    	{
            if (cell.Value == DBNull.Value || cell.Value == null)
                continue;
            if (cell.Value.ToString().Contains(textToFind)) {
                containingCells.Add(cell);
            }
    	}	
    }
    if (containingCells.Count > 0)
    	dataGridView1.CurrentCell=
                containingCells[currentContainingCellListIndex++];
};
//In the Event handler of FindNext button set the next containingCell as
//the current cell of DataGridView
ButtonFindNext.Click += (sender, args) => {
    if (currentContainingCellListIndex < containingCells.Count ())
        dataGridView1.CurrentCell=
                containingCells[currentContainingCellListIndex++];
};
 
//Find using LINQ
FindUsingLinqButton.Click += (sender, args) => {
    currentContainingCellListIndex=0;
    containingCells = dataGridView1.Rows.OfType<DataGridViewRow>().SelectMany (
    	dgvr => dgvr.Cells.OfType<DataGridViewCell>().Where (dgvc => 
    		dgvc.Value != DBNull.Value && dgvc.Value != null &&
    		dgvc.Value.ToString().Contains(textToFind))).ToList();
//If query syntax is required
//containingCells = (from row in dataGridView1.Rows.OfType<DataGridViewRow>()
//      from cell in row.Cells.OfType<DataGridViewCell>()
//	  where cell.Value != DBNull.Value && cell.Value != null &&
//	        cell.Value.ToString().Contains("S")
//	  select cell).ToList();
    if (containingCells.Count > 0)
        	dataGridView1.CurrentCell=
                containingCells[currentContainingCellListIndex++];
    };
}
  Permalink  
v4
Comments
codeBegin at 8-Jun-12 2:20am
   
good one 5!
VJ Reddy at 8-Jun-12 2:58am
   
Thank you, codeBegin :)
Manas Bhardwaj at 8-Jun-12 5:21am
   
well explained :) +5
VJ Reddy at 8-Jun-12 5:59am
   
Thank you, Manas :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 7 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100