protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = TaoDuLieu();
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (txtName.Text.Trim().Equals(string.Empty))
{
return;
}
DataTable dt = TaoDuLieu();
DataTable dtSearch = TaoDuLieu();
dtSearch.Clear();
string nameSearch = txtName.Text.Trim();
int n = dt.Rows.Count;
for (int i = 0; i < n; i++)
{
string getName = (string)dt.Rows[i]["Ten"];
if (getName.IndexOf(nameSearch, StringComparison.CurrentCultureIgnoreCase) >= 0)
{
DataRow drcopy = dtSearch.NewRow();
drcopy["Ten"] = dt.Rows[i]["Ten"];
drcopy["Tuoi"] = dt.Rows[i]["Tuoi"];
drcopy["Email"] = dt.Rows[i]["Email"];
dtSearch.Rows.Add(drcopy);
}
}
GridView1.DataSource = dtSearch;
GridView1.DataBind();
}
private DataTable TaoDuLieu()
{
DataTable dt = new DataTable();
DataColumn colTen = new DataColumn("Ten", typeof(string));
DataColumn colTuoi = new DataColumn("Tuoi", typeof(int));
DataColumn colEmail = new DataColumn("Email", typeof(string));
dt.Columns.Add(colTen);
dt.Columns.Add(colTuoi);
dt.Columns.Add(colEmail);
dt.Rows.Add(TaoMotRow(dt, "Nguyen Thi Lanh", 1987, "lanh_39tt@yahoo.com"));
dt.Rows.Add(TaoMotRow(dt, "Ngo Tan Nghia", 1987, "erick_frankie@yahoo.com.vn"));
dt.Rows.Add(TaoMotRow(dt, "Lê Thiện Nhật Quang", 1989, "admincnttvn@gmail.com"));
dt.Rows.Add(TaoMotRow(dt, "Nguyen Ngoc Thanh", 1986, "ngocthanh.t2@gmail.com"));
dt.Rows.Add(TaoMotRow(dt, "Quach Quang Thien", 1987, "quachquangthien_2710@yahoo.com"));
dt.Rows.Add(TaoMotRow(dt, "Nguyen Thanh Xuan", 1987, "xuan12k@gmail.com"));
dt.Rows.Add(TaoMotRow(dt, "Nguyen Thi Thanh Hai", 1989, "shiningstar30_03@yahoo.com"));
return dt;
}
private DataRow TaoMotRow(DataTable table, string ten, int tuoi, string email)
{
DataRow dr = table.NewRow();
dr["Ten"] = ten;
dr["Tuoi"] = 2010 - tuoi;
dr["Email"] = email;
return dr;
}
}