Click here to Skip to main content
15,888,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am was create one Gridview table through code behind,
In the Gridview table one column is for days counting from between two dates,
For the columns formate i wrote code in RowDataBound,
so in that Days column format is not converting,
It was showing Days.HH MM SS FFF also

pls. any one help me ..

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
    {
       // Gridview Data binding 
        DataClassesDataContext db = new DataClassesDataContext();
          var sumo = from i in db.Chech_ins
                     let Dys = DateTime.Now - i.IN_Date
                     select new
                   {   Gues_Name = i.Room_num,
                       IN_Date= i.IN_Date, 
                       Today_Date=DateTime.Now,
                       Days = Dys,
                       Total = i.Tariff * 2,
                   };
        GridView1.DataSource = sumo;
        GridView1.DataBind();
    
    }



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

       // for change the column formate
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
    
            e.Row.Cells[1].Text = Convert.ToDateTime(e.Row.Cells[1].Text).ToString("dd-MM-yyyy");
            e.Row.Cells[2].Text = Convert.ToDateTime(e.Row.Cells[2].Text).ToString("dd-MM-yyyy");
            e.Row.Cells[3].Text = double.Parse(e.Row.Cells[3].Text).ToString();
            e.Row.Cells[4].Text = Convert.ToInt32(e.Row.Cells[4].Text).ToString();
        }
    }
Posted
Updated 1-May-17 9:55am
v2

try
Dys = DateDiff(DateInterval.Day, DateTime.Now, i.IN_Date)
instead of
Dys = DateTime.Now - i.IN_Date
 
Share this answer
 
Comments
Member 11917879 1-May-17 15:50pm    
thank s for reply sir
but
this function not working here
Based on your post description, look like the code is query against a List / Enumerable object and not LINQ to Entities because it should had thrown an error. Anyway, try below.

C#
let Dys = DateTime.Now.Subtract(i.IN_Date).Days


If you end up changing the code to write LINQ to Entities query, then use the below code.
C#
let Dys = DbFunctions.DiffDays(i.IN_Date, DateTime.Now)
 
Share this answer
 
Comments
Member 11917879 2-May-17 1:58am    
it is working with small change
let dys = DateTime.Now.Subtract(i.IN_Date.Value).Days

thanku so much
Bryian Tan 2-May-17 2:02am    
oh ok, I guess IN_Date is a NullAble type. Glad that you got it to work.

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