Compare 2 Rows , cell values , and highlight if Row2 cell values has the least value red

pcwh1 21 Reputation points
2021-10-25T20:12:47.857+00:00

Hi I have a simple sql view showing on a grid view with 2 rows and 4 columns ( 3 columns with values) :

I need help highlighting if Cells in Row2 are less than Cells in Row1 in red.

143573-grid.png

incomplete code .. but want to compare Row1 and Row 2 and highlight cells value in row 2 that are less than row1

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)  
    {  
        if (e.Row.RowType == DataControlRowType.DataRow)  
        {  
            if (e.Row.RowType == DataControlRowType.DataRow)  
            {  
                  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,203 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2021-10-25T22:32:14.933+00:00

    If you just have two rows then this should work:

       public partial class Form1 : Form {  
       	public Form1() {  
       		InitializeComponent();  
       	}  
         
       	private void Form1_Load(object sender, EventArgs e) {  
       		DataTable table = new DataTable();  
         
       		table.Columns.Add("TOTAL_ORDERS");  
       		table.Columns.Add("ORANGE");  
       		table.Columns.Add("GREEN");  
       		table.Columns.Add("BLUE");  
         
       		table.Rows.Add(new[] { "TOTAL ORDERS", "22", "153", "84"});  
       		table.Rows.Add(new[] { "TOTAL ON HAND", "100", "95", "165" });  
         
       		dataGridView1.AllowUserToAddRows = false;  
       		dataGridView1.RowPrePaint += DataGridView1_RowPrePaint;  
       		dataGridView1.DataSource = table;			  
       	}  
         
       	private void DataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {  
       		if (!e.IsLastVisibleRow)   
       			return;  
         
       		DataGridViewRow currentRow = dataGridView1.Rows[e.RowIndex];  
       		DataGridViewRow previousRow = dataGridView1.Rows[e.RowIndex - 1];  
         
       		for (int column = 1; column < dataGridView1.Columns.Count; column++) {  
       			DataGridViewCell currentCell = currentRow.Cells[column];  
       			DataGridViewCell previousCell = previousRow.Cells[column];  
         
       			if (int.Parse((string)currentCell.Value) < int.Parse((string)previousCell.Value)) {  
       				currentCell.Style.BackColor = Color.Red;  
       			}  
       		}  
       	}  
       }  
    

    Result:
    143536-image.png

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. pcwh1 21 Reputation points
    2021-10-27T20:10:31.67+00:00

    This is great !! Thanks a lot @P a u l

    I did however make an SQL table to display that table on a gridview , which option would be best to use?

    so far I have been using GridView1_RowDataBound1(object sender, GridViewRowEventArgs e) to highlight text , which was easy .

    Not sure if you can understand my question ,

    gridview

    0 comments No comments