【发布时间】: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