views:

161

answers:

1

Hi,

I am using asp.net 2.0 and C#.

I have a generic list,

List<EmployeeInfo> empInfoList; 

this list is loaded with the employee information. Now, I want to filter this list with the textbox value. Which is "EmploeeName".

I have to filter this list with the employeeName, and bind it to the gridview again.

I am not sure how can I do that. Please help.

Thanks in advance.

+5  A: 

As you're using .Net2.0 you can't use LINQ, however you can use a delegate and the FindAll method:

string criteria = EmployeeName.Text.Trim().ToLower();
List<EmployeeInfo> resultList = empInfoList.FindAll(
   delegate(EmployeeInfo p)
   {
      return p.EmployeeName.ToLower().Contains(criteria);
   }
);
GenericTypeTea
what pros and cons comparing with foreach loop?
Arseny
@Arseny - read this here: http://stackoverflow.com/questions/674632/generic-list-findall-vs-foreach
GenericTypeTea
Seems like perfect to me but I am getting Null record in resultlist. When I have placed break point there I found that it's not going to the inner braket of delegate. But my empInfoList have 24 records.Please suggest.
Rahul
I don't think breaks inside the delegate get hit unless you step into the delegate from a line break on the same line as FindAll. I don't understand why you're getting null records as FindAll only returns records that match... Can you post up some code of what you've tried?
GenericTypeTea
List<EmployeeInfo> empInfoList = objCustomer.GetEmployees(user.PasswordQuestion, ref errorMsg);List<EmployeeInfo> resultList = empInfoList.FindAll( delegate(EmployeeInfo p) { return p.Name == txtSearchEmployee.Text.Trim(); } ); grvEmployees.DataSource = resultList; grvEmployees.DataBind();
Rahul
Please let me know if any other information is required.
Rahul
Put in a breakpoint before you set the DataSource and see if any of the items in resultList are null before you set the DataSource.
GenericTypeTea
I have checked the same, empInfoList is giving me 24 revcords. I have put "Cal" in the textbox, which is also coming correct. but when breakpoint comes to the delegate(EmployeeInfo p) part, it's going directly to Gridview bind statement, and resultList is returning Null records.
Rahul
Try the code I added and let me know if it still does the same thing or if it works without the FindAll method.
GenericTypeTea
You're not disposing the empInfoList are you?
GenericTypeTea
This seems to be working fine, but it is working when user has entered the excat name, I have made it as a search box, I want to use something like "like" operator.
Rahul
Ahh... When you say it's returning Null records, do you mean that the COUNT of the resultList is 0?
GenericTypeTea
If you remember, today only you answered me the question in which I questioned about filling the dataset with list view, that is working absolutely fine but I can't assign dataset to gridview because their parameters are not same. That's why I thought to do the same with the list.
Rahul
yes , count is 0
Rahul
Rahul - you confused me there. I've fixed the answer, let me know if it's any better. Don't use the word null in place of 0. They're not the same thing. That's why I didn't understand what the problem was.
GenericTypeTea
Actually, Gridview has predefined template, and Dataset conatins more column which is leading in error. but this list EmployeeInfo is returning me a correct column name with correct formating. That's the reason I want to go ahead with the list.
Rahul
Try my updated code.
GenericTypeTea
Absolute perfect.. Thanks a lot for your time and effort.. :) Thanks again :) :)
Rahul
No worries. Any time.
GenericTypeTea