locked
Highlight GridView row with least cell value RRS feed

  • Question

  • User1858009984 posted

    I have data binding in asp.net grid view control and i want to highlight a row with least cell value. So how to do this kind of task. i tried several ways but nothing worked and i am stuck with this.

    Monday, November 21, 2016 11:57 AM

Answers

  • User3690988 posted

    You could pull the minimum value in the SELECT routine, along with your data.  Then in the GridView's RowDataBound event compare the values, and if they match set the Row's BackColor.  The SQL could be something like this:

    DECLARE @minDate date = (SELECT Min(BirthDate)  FROM [Employees]); SELECT EmployeeID, LastName, FirstName, Title, BirthDate, HireDate, @minDate as minDate FROM [Employees]

    The RowDataBound event could be this:

      Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
          If e.Row.Cells(5).Text = e.Row.Cells(7).Text Then
            e.Row.BackColor = Drawing.Color.Aqua
          End If
        End If
      End Sub

    How I would get the minimum values depends on how much data I expect to pull, I probably would set it to an output parameter of a StoredProcedure, but what I did would work.

    How I compare values depends on how the GridView if DataBound.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 1:06 PM
  • User-1034726716 posted

    What have you tried? Can we see your exiting code? 

    Normally, the RowDataBound event will be used to manipulate the cells.

    protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //your code here
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 1:07 PM
  • User1564875471 posted

    Alternatively, you can handle the DataBound event of the Gridview which fires at the end of the bidning and then you iterate over all the rows and get the minum value then highligh it.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGrid();
                }
    
            }
    
            private void BindGrid()
            {
                grd1.DataSource = new int[] { 50, 30, 20, 100,20 };
                grd1.DataBind();
            }
    
            protected void grd1_DataBound(object sender, EventArgs e)
            {
                int targetRowIndex = 0;
                int min = int.MaxValue;
                int targetColumnIndex = 0;//we are highligting the first cell
    
                for (int rowIndex=0; rowIndex < grd1.Rows.Count; rowIndex++)
                {
                    int cellVal = int.Parse(grd1.Rows[rowIndex].Cells[targetColumnIndex].Text);
                    if (cellVal < min)
                    {
                        min = cellVal;
                        targetRowIndex = rowIndex;
                    }
                }
                grd1.Rows[targetRowIndex].Cells[targetColumnIndex].BackColor = System.Drawing.Color.Yellow;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 1:08 PM

All replies

  • User3690988 posted

    You could pull the minimum value in the SELECT routine, along with your data.  Then in the GridView's RowDataBound event compare the values, and if they match set the Row's BackColor.  The SQL could be something like this:

    DECLARE @minDate date = (SELECT Min(BirthDate)  FROM [Employees]); SELECT EmployeeID, LastName, FirstName, Title, BirthDate, HireDate, @minDate as minDate FROM [Employees]

    The RowDataBound event could be this:

      Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
          If e.Row.Cells(5).Text = e.Row.Cells(7).Text Then
            e.Row.BackColor = Drawing.Color.Aqua
          End If
        End If
      End Sub

    How I would get the minimum values depends on how much data I expect to pull, I probably would set it to an output parameter of a StoredProcedure, but what I did would work.

    How I compare values depends on how the GridView if DataBound.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 1:06 PM
  • User-1034726716 posted

    What have you tried? Can we see your exiting code? 

    Normally, the RowDataBound event will be used to manipulate the cells.

    protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //your code here
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 1:07 PM
  • User1564875471 posted

    Alternatively, you can handle the DataBound event of the Gridview which fires at the end of the bidning and then you iterate over all the rows and get the minum value then highligh it.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGrid();
                }
    
            }
    
            private void BindGrid()
            {
                grd1.DataSource = new int[] { 50, 30, 20, 100,20 };
                grd1.DataBind();
            }
    
            protected void grd1_DataBound(object sender, EventArgs e)
            {
                int targetRowIndex = 0;
                int min = int.MaxValue;
                int targetColumnIndex = 0;//we are highligting the first cell
    
                for (int rowIndex=0; rowIndex < grd1.Rows.Count; rowIndex++)
                {
                    int cellVal = int.Parse(grd1.Rows[rowIndex].Cells[targetColumnIndex].Text);
                    if (cellVal < min)
                    {
                        min = cellVal;
                        targetRowIndex = rowIndex;
                    }
                }
                grd1.Rows[targetRowIndex].Cells[targetColumnIndex].BackColor = System.Drawing.Color.Yellow;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2016 1:08 PM