Solved

DataGrid Search

Posted on 2006-06-20
4
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

734 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