Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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();
       
    }
   
}

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....
Posted
Updated 18-Jun-14 18:45pm
v3
Comments
DamithSL 19-Jun-14 0:44am    
what is the .net framework version you are using?
what is the column data type of todate in your database table?
can't you change the select sql statement of GetTaxdetails method to return data maching your condition?
Bh@gyesh 19-Jun-14 0:46am    
here, you need to write this condition in SQL query or SP from which you get datatable.
Pass date parameter in query or sql so you will get result depending on your condition. and then bind result set to grid.
santhu888 19-Jun-14 0:48am    
i dont need result from SP
Bh@gyesh 19-Jun-14 0:51am    
What is written in your "GetTaxdetails()" function? PAss parameter in this function

You haven't answer to my comments, below code will work for .net 3.5 + with LINQ
C#
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();

you can use r.Field<DateTime>("todate") <DateTime.Now.AddDays(15) if you have datetime column.
 
Share this answer
 
v3
Hello ,
You can use LINQ. 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
 
Share this answer
 
Update your query in GetTaxDetails so that it does not return rows that fail the IF.
 
Share this answer
 
create dataview using datatable and filter row into dataview then convert it to table and bind that table to gridview

C#
DataTable dtbind = new DataTable();
           dtbind = objvehicleBAL.GetTaxdetails();
           DataView dv = new DataView(dtbind);
           dv.RowFilter = "Date = #12/02/2014#";


               GVTax.DataSource = dv.ToTable();
               GVTax.DataBind();
 
Share this answer
 
I had solved my problem using this modifications in my code

C#
dttableNew  = dttableOld.Clone();

            foreach (DataRow drtableOld in dttableOld.Rows)
            {
               if (/*put some Condition */)
               {
                  dtTableNew.ImportRow(drtableOld);
               }
            }

            GVTax.DataSource = dtTableNew;

           GVTax.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900