Windows Develop Bookmark and Share   
 index > Windows Forms General > Incremental search in GridView by sorted column
 

Incremental search in GridView by sorted column

Hi,
at our project we have specific request on search feature in datagridview. When user starts typing in datagrid we have to perform incremental search in selected column (in THE column that is sorted) as user types. At the same time the grid should scroll through records the datagrid. I know the similar functionality in that use case is achieved via filter, but customer want to have all the items in the grid while searching for the record.

is there any easier way to implement this than i mention lower?

It's just a first draft because i will have to extend this code so that typed string is somehow "visible" to the user)
private string searchString = "";

 private void grdKlienti_KeyPress(object sender, KeyPressEventArgs e) 
{
            searchString += e.KeyChar;
            int sortColumn = GetSortedColumnIX(grdKlienti);
            int scrollTo;

            for (scrollTo = 0; scrollTo < grdKlienti.RowCount; scrollTo++)
            {
                if (grdKlienti.Rows[scrollTo].Cells[sortColumn].Value.ToString().StartsWith(searchString))
                {
                    break;
                }
            }
            grdKlienti.FirstDisplayedScrollingRowIndex = scrollTo;<br/>

 }



The GetSortedColumnIX just returns index of the column that is sorted.
  • Edited byKalaz.Net Tuesday, July 14, 2009 9:42 AMformatting
  • Edited byKalaz.Net Tuesday, July 14, 2009 9:45 AM
  • Edited byKalaz.Net Tuesday, July 14, 2009 9:40 AMformatting
  • Changed TypeAland LiMSFT, ModeratorWednesday, July 22, 2009 11:59 AMNo reply
  • Changed TypeKalaz.Net Thursday, July 23, 2009 11:07 AMHave time to answer
  • Edited byKalaz.Net Tuesday, July 14, 2009 9:44 AM
  •  
Kalaz.Net  Tuesday, July 14, 2009 9:38 AM

Hi Kalaz.Net,

You can use binary search to speed up searching. This is the code snippet:
//Search class, assume the sort type is ascending.

public static class QuickSearch

{

public static int Search(DataGridView grid, int sortedColumnIndex, string startString)

{

int start = 0;

int end = grid.Rows.Count - 1;

return Search(grid, sortedColumnIndex, startString, start, end);

}

private static int Search(DataGridView grid, int sortedColumnIndex, string startString, int start, int end)

{

//Not found

if (string.IsNullOrEmpty(startString) || start > end) return -1;

//Get the string of the middle cell.

int midPos = (start + end) / 2;

object cellVal = grid[sortedColumnIndex, midPos].Value;

string cellString = cellVal == null ? "" : cellVal.ToString().Trim();

//Handle start == end

if (start == end)

return cellString.StartsWith(startString) ? start : -1;

//Handle blank.

if(cellString == "")

{

if(start < end)

{

//Search in the follow items.

return Search(grid,sortedColumnIndex,startString,start + 1, end);

}

else

return -1;//Not found.

}

if (cellString.Length >= startString.Length)

{

cellString = cellString.Substring(0, startString.Length);

int diff = cellString.CompareTo(startString);

if (diff == 0)

return midPos;//This means cellString starts with startString.

else if (diff > 0)

//cellString > startString, search in the first half.

return Search(grid, sortedColumnIndex, startString, start, midPos - 1);

else

//cellString < startString, search in the last half.

return Search(grid, sortedColumnIndex, startString, midPos + 1, end);

}

else

{

string newStartString = startString.Substring(0, cellString.Length);

int diff = newStartString.CompareTo(cellString);

if (diff >= 0)

//startString > cellString, search in the last half.

return Search(grid, sortedColumnIndex, startString, midPos + 1, end);

else

//startString < cellString, search in the first half.

return Search(grid, sortedColumnIndex, startString, start, midPos - 1);

}

}

}

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Unmarked As Answer byKalaz.Net Thursday, July 23, 2009 11:09 AM
  • Marked As Answer byKalaz.Net Thursday, July 23, 2009 11:41 AM
  • Marked As Answer byKalaz.Net Thursday, July 23, 2009 11:08 AM
  •  
Aland Li  Thursday, July 16, 2009 9:53 AM

Hi Kalaz.Net,

You can use binary search to speed up searching. This is the code snippet:
//Search class, assume the sort type is ascending.

public static class QuickSearch

{

public static int Search(DataGridView grid, int sortedColumnIndex, string startString)

{

int start = 0;

int end = grid.Rows.Count - 1;

return Search(grid, sortedColumnIndex, startString, start, end);

}

private static int Search(DataGridView grid, int sortedColumnIndex, string startString, int start, int end)

{

//Not found

if (string.IsNullOrEmpty(startString) || start > end) return -1;

//Get the string of the middle cell.

int midPos = (start + end) / 2;

object cellVal = grid[sortedColumnIndex, midPos].Value;

string cellString = cellVal == null ? "" : cellVal.ToString().Trim();

//Handle start == end

if (start == end)

return cellString.StartsWith(startString) ? start : -1;

//Handle blank.

if(cellString == "")

{

if(start < end)

{

//Search in the follow items.

return Search(grid,sortedColumnIndex,startString,start + 1, end);

}

else

return -1;//Not found.

}

if (cellString.Length >= startString.Length)

{

cellString = cellString.Substring(0, startString.Length);

int diff = cellString.CompareTo(startString);

if (diff == 0)

return midPos;//This means cellString starts with startString.

else if (diff > 0)

//cellString > startString, search in the first half.

return Search(grid, sortedColumnIndex, startString, start, midPos - 1);

else

//cellString < startString, search in the last half.

return Search(grid, sortedColumnIndex, startString, midPos + 1, end);

}

else

{

string newStartString = startString.Substring(0, cellString.Length);

int diff = newStartString.CompareTo(cellString);

if (diff >= 0)

//startString > cellString, search in the last half.

return Search(grid, sortedColumnIndex, startString, midPos + 1, end);

else

//startString < cellString, search in the first half.

return Search(grid, sortedColumnIndex, startString, start, midPos - 1);

}

}

}

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Unmarked As Answer byKalaz.Net Thursday, July 23, 2009 11:09 AM
  • Marked As Answer byKalaz.Net Thursday, July 23, 2009 11:41 AM
  • Marked As Answer byKalaz.Net Thursday, July 23, 2009 11:08 AM
  •  
Aland Li  Thursday, July 16, 2009 9:53 AM

Hi Kalaz.Net,

Please let me know if my code snippet helps. Based on my understanding, you want to find the row faster. Please feel free to tell me if I misunderstand you.

You said is there any easier way to implement this than i mention lower? Do you mean you want to simplify the code?

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, July 21, 2009 4:50 AM

Hi,

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 22, 2009 11:59 AM
I am sorry, I haven't had time up until now to report back. Your solution is good but i have to make a few adjustments, i will post back when it's ready.
Kalaz.Net  Thursday, July 23, 2009 11:10 AM

You can use google to search for other answers

Custom Search

More Threads

• Image Gone on minimize??? I'm using BitBlt
• Validation Problem
• Firing an event on another form
• Tab Control with Maximize button
• Focus loss
• Close a modal dialog when the user clicks else where
• Complex properties in the PropertyGrid
• How do you keep tooltips working when a control is moved to another form?
• HelpProvider Object Behavior
• Loose forcus when showdialog(Me) used