Để kiểm tra sự tồn tại của một cell trong DataTable bằng ngôn ngữ C# ta sử dụng hàm sau đây.
Đầu tiên cần khai báo namespace
using System.Data;
Tiếp theo sử dụng hàm sau
protected void Submit(object sender, EventArgs e)
{
DataTable dt =new DataTable();
dt.Columns.AddRange(new DataColumn[6] {new DataColumn("ID",typeof(int)),
new DataColumn("Acount",typeof(string)),
new DataColumn("Name",typeof(string)),
new DataColumn("Quarter",typeof(string)),
new DataColumn("FY",typeof(int)),
new DataColumn("Income_percent",typeof(int))});
dt.Rows.Add(1,"ABC","Ram","Q1", 2011, 50);
dt.Rows.Add(2,"XYZ","Hari","Q4", 2011, 35);
dt.Rows.Add(3,"ABC","Rohit","Q3", 2011, 40);
dt.Rows.Add(4,"ABC","Ram","Q2", 2011, 25);
dt.Rows.Add(5,"XYZ","Hari","Q3", 2011, 60);
bool exists = dt.Select().ToList().Exists(row => row["Quarter"].ToString().ToUpper() == txtQuarter.Text);
if (exists)
{
ClientScript.RegisterStartupScript(this.GetType(),"alert","alert('Record Exists.');",true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(),"alert","alert('Record Not Exists.');",true);
}
}
Ý tưởng là như vậy, các bạn sử dụng hàm trên linh hoạt một chút cho phù hợp với nhu cầu sử dụng.
Thân ái !