write a entity class
1
public 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
}
public class Student2
{3
private int id;4

5
public int Id6
{7
get { return id; }8
set { id = value; }9
}10
private string name;11

12
public string Name13
{14
get { return name; }15
set { name = value; }16
}17
private string score;18

19
public string Score20
{21
get { return score; }22
set { score = value; }23
}24
}A method to get some data
1
private 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
}
private 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
}比较方法获取匹配数据
1
private 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
}
private 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
else16
{17
return this.GetStudent();18
}19
return (Student[])al.ToArray(typeof(Student));20
}测试事件
1
protected void Button1_Click(object sender, EventArgs e)
2
{
3
this.GridView1.DataSource = this.Compare(this.TextBox1.Text);
4
this.GridView1.DataBind();
5
}
protected void Button1_Click(object sender, EventArgs e)2
{3
this.GridView1.DataSource = this.Compare(this.TextBox1.Text);4
this.GridView1.DataBind();5
}这个例子实现的功能是从GridView里查找和Textbox里模糊匹配的数据集


浙公网安备 33010602011771号