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: