Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGrid Search

Posted on 2006-06-20
4
Medium Priority
?
282 Views
Last Modified: 2010-04-16
What is the best way to search a datagrid?
I know of 2 ways:
Loop through the rows checking the values for the one that I am searching for,
Use a dataview.find method.

I would like to use a dataview method, but it requires that I sort the dataview.  If I sort the dataview, won't it return the wrong row index of my search field.  Ex:

If my datagrid lists appointments for a doctor and I am searching for patient number 3 which is 5th on the list.  If I sort the dataview by patient number, won't that give me the wrong index compared to my datagrid.  My datagrid is showing my appointments in order by appointment time.  Another thing, what if I don't know the names of my columns at the time I am trying to search a dataview???????????????

Any help is appreciated,
Kendal
0
Comment
Question by:gvector1
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 16948709
If you have a DataTable as the source of the view, you can search by using the Select method. Its something like:

dataTable.Select ("PatientNumber = " + patientNumber);

This returns an array of DataRows which meet the condition. You could dynamically create the condition string if you don't know the column name at design time.
0
 

Author Comment

by:gvector1
ID: 16950675
But I need to know the row #s containing the values entered for the search so that I can actually select those rows within the datagrid.  How can I go about doing that????
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 2000 total points
ID: 16950708
You need to go over all rows in the dataview and check if they have the row you need. Something lik:

int index = -1;
for (int i = 0; i < dataView.Count; i++)
{
  if (dataView[i] == rowToFind)
  {
      index = i;
      break;
  }
}

// index contains now the index of the row
0
 

Author Comment

by:gvector1
ID: 16950879
That's what I was wondering.  Whether looping through the rows in the table is an efficient method for this.  Thanks for the confimation.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question