onur
onur

Reputation: 6365

How can I compare date of two rows

I have Gridview and Datasource like this:

<asp:SqlDataSource ID="SqlDataSource10" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="SELECT * FROM [data.csv]"></asp:SqlDataSource>

    <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" DataSourceID="SqlDataSource10" GridLines="Vertical" 
        Width="695px" Height="130px" onrowdatabound="GridView3_RowDataBound">
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <Columns>
            <asp:BoundField DataField="H" HeaderText="H" 
                SortExpression="H" >
            <HeaderStyle Width="115px" />
            </asp:BoundField>
            <asp:BoundField DataField="V" HeaderText="V" 
                SortExpression="V" >
            <HeaderStyle Width="100px" />
            </asp:BoundField>
            <asp:BoundField DataField="F" HeaderText="F" 
                SortExpression="F" dataformatstring="{0:dd-MM-yyyy HH:mm}" >
            <HeaderStyle Width="180px" />
            </asp:BoundField>
            <asp:BoundField DataField="V" HeaderText="V" 
                SortExpression="V" dataformatstring="{0:dd.MM.yyyy HH:mm}" >
            <HeaderStyle Width="180px" />
            </asp:BoundField>

I use csv file for data. I have to change backcolor if date value is old(2 hours).

I can do that for first date column like this:

 DateTime dt;
  if (DateTime.TryParseExact(e.Row.Cells[1].Text, "dd.MM.YYYY HH:mm",
                         CultureInfo.InvariantCulture,
                         DateTimeStyles.None, out dt))
{
   if (dt <= DateTime.Now.AddHours(-2))
   {
       if (e.Row.Cells[0].Text.Contains("M"))
       {
           e.Row.Cells[1].BackColor = Color.LightCoral;
       }
   }

If first date column values older than 2 hours, backcolor changing.

I need to do this for 2. date column. But compare to first date value. If second date value older than 2 hours according to the first date value, backcolor changing:

enter image description here

How can I do that?

Upvotes: 2

Views: 1867

Answers (2)

Bayeni
Bayeni

Reputation: 1046

You can use TimeSpan class to calculate the difference between to date. You can do this OnRowDataBound event, see below

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            //Checking the RowType of the Row  
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DateTime firstDateValue = Convert.ToDateTime(e.Row.Cells[1].Text);
                DateTime secondDateValue = Convert.ToDateTime(e.Row.Cells[2].Text);

                 TimeSpan timespan = secondDateValue - firstDateValue; 

                if (timespan.Hours > 2)
                {
                    e.Row.BackColor = Color.Cyan;
                }
            }
        } 

Upvotes: 2

jmc
jmc

Reputation: 1729

You can put this when you populate your DataGridView

int index = dataGridView1.Rows.Add();
        dataGridView1.Rows[index].Cells[colFirstDate.Name].Value = DateTime.Now;
        dataGridView1.Rows[index].Cells[colSecondDate.Name].Value = DateTime.Now.AddHours(3);

        DateTime firstDate = Convert.ToDateTime(dataGridView1.Rows[index].Cells[colFirstDate.Name].Value);
        DateTime secondDate = Convert.ToDateTime(dataGridView1.Rows[index].Cells[colSecondDate.Name].Value);
        TimeSpan timespan = secondDate - firstDate;

        if (timespan.Hours > 2)
        {
            dataGridView1.Rows[index].Cells[colFirstDate.Name].Style.BackColor = Color.Gray;
        }

Upvotes: 2

Related Questions