Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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:
C#
//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++];
    };
}
 
Share this answer
 
v4
Comments
codeBegin 8-Jun-12 2:20am    
good one 5!
VJ Reddy 8-Jun-12 2:58am    
Thank you, codeBegin :)
Manas Bhardwaj 8-Jun-12 5:21am    
well explained :) +5
VJ Reddy 8-Jun-12 5:59am    
Thank you, Manas :)
Member 11419259 10-Jul-15 4:19am    
I was searching for somthing like this. thanx a lotttt... :) :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900