简体   繁体   English

如何比较两行的日期

[英]How can I compare date of two rows

I have Gridview and Datasource like this: 我有这样的Gridview和数据源:

<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. 我使用csv文件存储数据。 I have to change backcolor if date value is old(2 hours). 如果日期值较旧(2小时),则必须更改背景色。

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. 如果首个日期列的值早于2小时,则背景色会发生变化。

I need to do this for 2. date column. 我需要为2. date列执行此操作。 But compare to first date value. 但是要与第一个日期值进行比较。 If second date value older than 2 hours according to the first date value, backcolor changing: 如果根据第一个日期值,第二个日期值早于2小时,则背景色将更改:

在此处输入图片说明

How can I do that? 我怎样才能做到这一点?

You can use TimeSpan class to calculate the difference between to date. 您可以使用TimeSpan类计算迄今为止的时间差。 You can do this OnRowDataBound event, see below 您可以执行此OnRowDataBound事件,请参见下文

        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;
                }
            }
        } 

You can put this when you populate your DataGridView 您可以在填充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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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