Click here to Skip to main content
11,925,896 members (61,026 online)
Rate this:
Please Sign up or sign in to vote.
See more: string DataGridView

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
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) => {
    foreach (DataGridViewRow row in dataGridView1.Rows)
    	foreach (DataGridViewCell cell in row.Cells)
            if (cell.Value == DBNull.Value || cell.Value == null)
            if (cell.Value.ToString().Contains(textToFind)) {
    if (containingCells.Count > 0)
//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 ())
//Find using LINQ
FindUsingLinqButton.Click += (sender, args) => {
    containingCells = dataGridView1.Rows.OfType<DataGridViewRow>().SelectMany (
    	dgvr => dgvr.Cells.OfType<DataGridViewCell>().Where (dgvc => 
    		dgvc.Value != DBNull.Value && dgvc.Value != null &&
//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)
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 10 Jul 2015
Copyright © CodeProject, 1999-2015
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