question

pcwh1 avatar image
0 Votes"
pcwh1 asked pcwh1 commented

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

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)
             {



dotnet-csharp
grid.png (6.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Paul-5034 avatar image
1 Vote"
Paul-5034 answered pcwh1 commented

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



image.png (6.3 KiB)
image.png (7.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Paul-5034 here is the code I have tied, but got stuck with the cell comparison


 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     for (int i = 0; i < GridView2.Rows.Count - 1; i++)
     {
         for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++)
         {
             if (GridView2.Rows[i + 1].Cells[j] < GridView2.Rows[i].Cells[j])
             {
                 GridView2.Rows[i + 1].Cells[j].BackColor = Color.Red;
    
             }
         }
     }

0 Votes 0 ·
pcwh1 avatar image
0 Votes"
pcwh1 answered

This is great !! Thanks a lot @Paul-5034

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



capture.png (12.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.