简体   繁体   中英

how to calculate days between two dates using datatable and datarow in gridview?

public void BindGrid(int rowcount)
{
    btnsubmit.Visible = true;
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("Employee ID", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("Leave Type", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("From", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("To", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("No:of Days", typeof(String)));


    if (ViewState["CurrentData"] != null)
    {
        for (int i = 0; i < rowcount + 1; i++)
        {
            dt = (DataTable)ViewState["CurrentData"];
            if (dt.Rows.Count > 0)
            {
                dr = dt.NewRow();
                dr[0] = dt.Rows[0][0].ToString();

            }
        }
        dr = dt.NewRow();
        dr[0] = lblempID.Text;
        dr[1] = ddlleavetype.SelectedItem.Text.ToString();
        dr[2] = txtfrom.Text.ToString();
        dr[3] = txtto.Text.ToString();

        dt.Rows.Add(dr);

    }
    else
    {
        dr = dt.NewRow();
        dr[0] = lblempID.Text;
        dr[1] = ddlleavetype.SelectedItem.Text.ToString();
        dr[2] = txtfrom.Text.ToString();
        dr[3] = txtto.Text.ToString();

        dt.Rows.Add(dr);

    }

    // If ViewState has a data then use the value as the DataSource
    if (ViewState["CurrentData"] != null)
    {
        gvLeave.DataSource = (DataTable)ViewState["CurrentData"];
        gvLeave.DataBind();
    }
    else
    {
        // Bind GridView with the initial data assocaited in the DataTable
        gvLeave.DataSource = dt;
        gvLeave.DataBind();

    }
    // Store the DataTable in ViewState to retain the values
    ViewState["CurrentData"] = dt;


}

In my page having from date textbox and todate textbox..i want to calculate the between two dates ,please help me

If they are in a textbox you will need to parse them first using DateTime.Parse . This is under the assumption that the strings are in the correct format

DateTime myDate = DateTime.Parse(myDateString);

To calculate the days between two dates you can try this

TimeSpan span =date1-date2;
var days = span.TotalDays;

I'm not very familiar with the DataTable object, so take this answer as an idea. :-)

Why don't you specify the Rows with the actual type?

dt.Columns.Add(new System.Data.DataColumn("From", typeof(DateTime)));
dt.Columns.Add(new System.Data.DataColumn("To", typeof(DateTime)));
dt.Columns.Add(new System.Data.DataColumn("No:of Days", typeof(int)));

And after this you can Parse the data of txtfrom like dr[2] = DateTime.Parse(txtfrom.Text.ToString()) and assign it to your DataRow. Same goes for txtto.

After this, you are able to calculate the days between by using:

dr[4] = (int)((dr[2] -dr[3]).TotalDays);

If this doesn't work, try to save the parsed Dates in objects like so:

DateTime from = DateTime.Parse(txtfrom.Text.ToString());
DateTime to = ...;

TimeSpan t1 = to - from;
dt[4] = (int)(t1.TotalDays);

试试这个代码:

Var Day= DateTime.Parse(todate.Text).Subtract(DateTime.Parse(fromdate.Text)).Days;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM