Solved

DataGrid Search

Posted on 2006-06-20
4
245 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now