如何在Asp.net C#中绑定gridview的一些条件? [英] how to bind gridview with some conditions in Asp.net C#?
本文介绍了如何在Asp.net C#中绑定gridview的一些条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataTable dtbind = new DataTable();
dtbind = objvehicleBAL.GetTaxdetails();
for (int i = 0; i < dtbind.Rows.Count; i++)
{
DateTime dt1 = DateTime.ParseExact(dtbind.Rows[i]["todate"].ToString(), "dd/MM/yyyy", null);
if (dt1 < ((DateTime.Now.AddDays(15))))
{
GVTax.DataSource = dtbind.Rows[i];
GVTax.DataBind();
}
}
我在if()中写了条件。我想在网格中只绑定满意的行。我怎么写请帮助我紧急....
i had written my conditions in if(). I want to bind only satisfied rows in grid. How can I write please help me out its urgent....
推荐答案
你没有回答我的评论,下面的代码将适用于.net 3.5 + with LINQ
You haven't answer to my comments, below code will work for .net 3.5 + with LINQ
DataTable dtbind = new DataTable();
dtbind = objvehicleBAL.GetTaxdetails();
dtbind = dtbind.AsEnumerable()
.Where(r=>DateTime.ParseExact(r.Field<string>("todate"), "dd/MM/yyyy", null) < DateTime.Now.AddDays(15))
.CopyToDataTable();
GVTax.DataSource = dtbind;
GVTax.DataBind();
可以使用 r.Field< DateTime的>( TODATE)< DateTime.Now .AddDays(15)
如果你有datetime列。
you can use r.Field<DateTime>("todate") <DateTime.Now.AddDays(15)
if you have datetime column.
你好,
你可以使用LINQ
。试试这种方式
Hello ,
You can useLINQ
. Try this way
ateTime dt=DateTime.Now.AddDays(15);//first take one DateTime variable and set the value
//make the Where condition in your DataTable
var newdatasource = dtbind.Where(x => t.todate.Date < dt).ToList();
//now set the new DataSource to gridview
GVTax.DataSource = newdatasource
GVTax.DataBind();
谢谢
thanks
在GetTaxDetails中更新您的查询,以便它不会返回IF失败的行。
Update your query in GetTaxDetails so that it does not return rows that fail the IF.
这篇关于如何在Asp.net C#中绑定gridview的一些条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文