How to compare rows of cells in the Gridview, The gridview Cells

Source: Internet
Author: User

How to compare rows of cells in the Gridview, The gridview Cells

One problem I saw on the Forum was that in the Gridview control, we needed to compare the values of several columns. The original problem was as follows:

 

Prepare data in the database first:

 

Create table [dbo]. [RecordTime] (Id int identity (1, 1) primary key, Time1 DATETIME, Time2 DATETIME, Time3 DATETIME, Time4 DATETIME) goinsert into [dbo]. [RecordTime] VALUES ('2017-05-11 ', '2017-05-20', '2017-05-13 48 ', '2017-05-19 '), ('2017-05-10 48', '2017-05-01 02 ', '2017-05-20 ', '2017-05-20 '), ('2017-05-20', '2017-05-03 40', '2017-05-14 ', '2017-05-25 ') go create procedure [dbo]. [usp_RecordTime_GetAll] as select [Id], [Time1], [Time2], [Time3], [Time4] FROM [dbo]. [RecordTime] GOView Code


Write the GridView control on the aspx webpage:

 

Then the OnRowDataBound = "GridView1_RowDataBound" event can be implemented in the. aspx. cs code page:


. Aspx. cs code:

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


The running result is as follows:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.