arraylist寻找对象中匹配的数据

Posted on 2008-07-08 11:58  Ray Gu  阅读(906)  评论(1)    收藏  举报

write a entity class

 1public class Student
 2{
 3    private int id;
 4
 5    public int Id
 6    {
 7        get return id; }
 8        set { id = value; }
 9    }

10    private string name;
11
12    public string Name
13    {
14        get return name; }
15        set { name = value; }
16    }

17    private string score;
18
19    public string Score
20    {
21        get return score; }
22        set { score = value; }
23    }

24}

A method to get some data
 1private Student[] GetStudent()
 2    {
 3        Student[] students = new Student[2];
 4        Student stu = new Student();
 5        stu.Id = 1;
 6        stu.Name = "zhangsan";
 7        stu.Score = "60";
 8        students.SetValue(stu, 0);
 9        Student stu2 = new Student();
10        stu2.Id = 2;
11        stu2.Name = "lisi";
12        stu2.Score = "80";
13        students.SetValue(stu2, 1);
14        return students;
15    }

比较方法获取匹配数据
 1private Student[] Compare(string name)
 2    {
 3        Student[] stu = this.GetStudent();
 4        System.Collections.ArrayList al = new System.Collections.ArrayList();
 5        if (!String.IsNullOrEmpty(name))
 6        {
 7            foreach (Student stud in stu)
 8            {
 9                if (stud.Name.IndexOf(name) > -1)
10                {
11                    al.Add(stud);
12                }

13            }

14        }

15        else
16        {
17            return this.GetStudent();
18        }

19        return (Student[])al.ToArray(typeof(Student));
20    }

测试事件
1protected void Button1_Click(object sender, EventArgs e)
2    {
3        this.GridView1.DataSource = this.Compare(this.TextBox1.Text);
4        this.GridView1.DataBind();
5    }

这个例子实现的功能是从GridView里查找和Textbox里模糊匹配的数据集

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3