如何比较两行的日期

时间:2015-04-09 06:48:42

标签: c# asp.net datetime gridview

我有这样的Gridview和Datasource:

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

我使用csv文件作为数据。如果日期值很旧(2小时),我必须更改背景颜色。

我可以在第一个日期栏中这样做:

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

如果第一个日期列值超过2小时,则背景颜色会发生变化。

我需要为2. date列执行此操作。但与第一个日期值相比。如果第二个日期值超过2小时,则根据第一个日期值,背景颜色变化:

enter image description here

我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以使用TimeSpan类来计算到目前为止的差异。您可以执行此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;
                }
            }
        } 

答案 1 :(得分:2)

您可以在填充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;
        }