locked
highlight lowest value in gridview RRS feed

  • Question

  • User66371569 posted

    Hi

    I have gridview

    id mmm ssss xxx
    1 12 15 17
    2 20 18 30

    just what i want  is  color background of cell  lowest value      but  by row

    example  in gridvrew

    first row    12        15            17

    12 must be green back color

    second row   20       18     30

    18 must back color green

    Sunday, April 25, 2021 4:43 PM

All replies

  • User-1716253493 posted
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim minval As Integer = 0
                For i As Integer = 1 To 3
                    If i = 1 Then
                        minval = e.Row.Cells(i).Text
                    Else
                        If minval > e.Row.Cells(i).Text Then
                            minval = e.Row.Cells(i).Text
                        End If
                    End If
                Next
                For i As Integer = 1 To 3
                    If minval = e.Row.Cells(i).Text Then
                        e.Row.Cells(i).BackColor = System.Drawing.Color.Green
                    End If
                Next
            End If
        End Sub

    Sunday, April 25, 2021 7:39 PM
  • User66371569 posted

     Hi

    I don,t want to specify in for   1 to ..............

    cause my giridview is dynamic   columns sometimes   3 columns   and  sometimes 4   and ...............

    Sunday, April 25, 2021 9:46 PM
  • User66371569 posted
    i checked it its not highlighting
    Monday, April 26, 2021 8:21 AM
  • User1535942433 posted

    Hi thepast,

    Do you care  of jquery? I have created a demo to foreach gridview row and column.Just like this:

    <script>
            $(function () {
                var totalRowCount = $("[id*=GridView1] tr").not(':first').length;
                var totalColumnCount = ($("[id*=GridView1] tr td").length) / totalRowCount;
                for (var i = 1; i <= totalRowCount; i++) {
                    var min = $("[id*=GridView1] tr:eq('" + i + "') td:eq(1)").text();
                    var result = min;
                    var xmin = 1;
                    for (var x = 2; x <= totalColumnCount; x++) {
                        var temp = $("[id*=GridView1] tr:eq('" + i + "') td:eq('" + x + "')").text();
                        if (temp < min) {
                            result = temp;
                            xmin = x;
                        }
                        $("[id*=GridView1] tr:eq('"+ i +"')  td:eq('" + xmin + "')").css('background', 'green');
                    }
                }
            })
        </script> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" /> <asp:BoundField DataField="mmm" HeaderText="mmm" /> <asp:BoundField DataField="sss" HeaderText="sss" /> <asp:BoundField DataField="xxx" HeaderText="xxx" /> </Columns> </asp:GridView>

    Code-behind:

    If Not Page.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("Id", GetType(Int32))
        dt.Columns.Add("mmm", GetType(Int32))
        dt.Columns.Add("sss", GetType(Int32))
        dt.Columns.Add("xxx", GetType(Int32))
        Dim dr As DataRow = dt.NewRow()
        dr("Id") = 1
        dr("mmm") = 12
        dr("sss") = 15
        dr("xxx") = 17
        dt.Rows.Add(dr)
        Dim drs As DataRow = dt.NewRow()
        drs("Id") = 2
        drs("mmm") = 20
        drs("sss") = 18
        drs("xxx") = 30
        dt.Rows.Add(drs)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If

    Result:

    Best regards,

    Yijing Sun

    Monday, April 26, 2021 9:03 AM
  • User66371569 posted

    Thank you   Yij sun

    I tested not worked with me

    what i want  i have that gridview

    just i wan it start check from column 1 to  ....   (here i  don,t know numbers of column because its dynamic)

    and check minimum value by row which start from column 1 

    thank you

    Monday, April 26, 2021 10:06 AM
  • User-1716253493 posted

     Hi

    I don,t want to specify in for   1 to ..............

    cause my giridview is dynamic   columns sometimes   3 columns   and  sometimes 4   and ...............

    You can use 1 to

    For i As Integer = 1 To e.Row.Cells.Count - 1

    because first column is id

    Monday, April 26, 2021 2:28 PM
  • User-1716253493 posted

    i checked it its not highlighting

    Are the columns boundfield?

    Monday, April 26, 2021 2:31 PM
  • User66371569 posted

    not bound filed

    auto generate  from query

    Monday, April 26, 2021 9:21 PM
  • User1535942433 posted

    Hi thepast,

    You could for loop with gridview row and column.Just like this:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            For Each row As GridViewRow In GridView1.Rows
                For i As Integer = 2 To GridView1.Columns.Count - 1
    .....
    Next Next End If End Sub

    Best regards,

    Yijing Sun

    Tuesday, April 27, 2021 9:58 AM