码迷,mamicode.com
首页 > 其他好文 > 详细

在Gridview如何进行每行单元格比较

时间:2015-06-12 14:37:39      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

有在论坛上看到一个问题,就是在Gridview控件中,需要对几个列的数值进行比较,原问题如下:
技术分享

 

先在数据库中准备数据:

技术分享

 

技术分享
CREATE TABLE [dbo].[RecordTime]
(
    Id INT IDENTITY(1,1) PRIMARY KEY,
    Time1 DATETIME,
    Time2 DATETIME,
    Time3 DATETIME,
    Time4 DATETIME
)
GO
INSERT INTO [dbo].[RecordTime] VALUES (2015-05-11 09:48,2015-05-20 10:08,2015-05-13 14:48,2015-05-19 08:48),
                                      (2015-05-10 14:48,2015-05-01 10:02,2015-05-20 12:48,2015-05-20 10:20),
                                      (2015-05-20 10:31,2015-05-03 10:40,2015-05-14 10:40,2015-05-25 09:48)
  GO

 CREATE PROCEDURE [dbo].[usp_RecordTime_GetAll]
 AS
    SELECT [Id],[Time1],[Time2],[Time3],[Time4] FROM [dbo].[RecordTime]
GO
View Code


在aspx网页上写好GridView控件:
技术分享

 

然后可以在.aspx.cs代码页中实现OnRowDataBound="GridView1_RowDataBound"事件:

技术分享


.aspx.cs代码:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
using System.Data;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    RecordTime rt = new RecordTime();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Data_Binding();
    }

    private void Data_Binding()
    {
        this.GridView1.DataSource = rt.GetRecordTime();
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) return;

        DataRowView drv = (DataRowView)e.Row.DataItem;
        List<DateTime> lDT = new List<DateTime>();
        lDT.Add(Convert.ToDateTime(drv["Time1"]));
        lDT.Add(Convert.ToDateTime(drv["Time2"]));
        lDT.Add(Convert.ToDateTime(drv["Time3"]));
        lDT.Add(Convert.ToDateTime(drv["Time4"]));
        DateTime dt = lDT.Max<DateTime>();
        int i = lDT.IndexOf(dt);

        int actualIdx = i + 1;

        e.Row.Cells[actualIdx].ForeColor = Color.FromName("red");
    }
}
View Code


运行得到的效果:
技术分享

 

在Gridview如何进行每行单元格比较

标签:

原文地址:http://www.cnblogs.com/insus/p/4571432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!