【问题标题】:How can I compare date of two rows如何比较两行的日期
【发布时间】:2015-06-14 09:55:31
【问题描述】:

我有这样的 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. 日期列执行此操作。但与第一个日期值相比。如果第二个日期值比第一个日期值早 2 小时,则背景颜色变化:

我该怎么做?

【问题讨论】:

    标签: c# asp.net datetime gridview


    【解决方案1】:

    您可以使用 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;
                    }
                }
            } 
    

    【讨论】:

    • 如何使用 Gridview 行?
    • @Ric 我已经编辑了答案,请检查是否有帮助。
    • 谢谢,这对我有用。但是当这样的日期值(小时和分钟 00)不起作用时:09.09.2015 00:00。我该如何解决?
    • 不是比较天数,而是比较小时数。如果超过 1 天,如果小时值不超过 2 小时,则不起作用。
    • 谢谢。但是数学出现错误:Error 1 The call is ambiguous between the following methods or properties: 'System.Math.Round(double)' and 'System.Math.Round(decimal)' 和前 60 个:Error 2 The operation overflows at compile time in checked mode
    【解决方案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;
            }
    

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多