Solved

Gridview and conditional formatting...

Posted on 2014-04-07
6
278 Views
Last Modified: 2014-04-30
A girdview populated via a stored procedure has two columns.

CustomerName and PastDueYN


Question: How can I make CustomerName field shaded light blue for customers with PastDue = True (similar to the conditional formatting in MS Access).

Alos, is there a way to keep PastDueYN invisible?

Thank you,
0
Comment
Question by:Mike Eghtebas
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39984570
You would need to handle the RowDataBound event. In there you can check the values of the columns in the current row, and then apply a background colour based on the values.

You should be able to hide the column easily enough. How are you currently defining the columns for your GridView? Are you letting them be auto-generated?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 39984814
Hi Carl,

Thank you your post. I located:
stackoverflow.com/questions/16799166/formatting-programmatically-changing-the-row-color-of-a-gridview-control

But it is in C#, I will try to get my control's name and attempt to apply to my project:
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        //in every row I would like to check the text of the second cell
        if (GridView1.Rows[i].Cells[1].Text == "C6N")
        {
            e.Row.BackColor = System.Drawing.Color.Red;

        }
    }

}

Open in new window


I will run by the code I come up with.

Mike
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 39985366
There are a couple of additional checks you would need to do. If you are letting the grid generate the columns itself, then it will create a Checkbox for the bit column, so you have to check the state of the associated checkbox control. Also, you only want your code to run if it is a DataRow that is being bound:
    Protected Sub Gridview1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Gridview1.RowDataBound

        Dim cb As CheckBox

        If e.Row.RowType = DataControlRowType.DataRow Then
            cb = CType(e.Row.Cells(1).Controls(0), CheckBox)
            If cb IsNot Nothing And cb.Checked Then
                e.Row.Cells(0).BackColor = Drawing.Color.Blue
            End If
        End If

    End Sub

Open in new window

0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 300 total points
ID: 39987645
You could determine the value of the PastDue field in SQL and return a CssClass name accordingly then set the CssClass of the gridview field (Template Field) to evaluate the  CssClass string returned by the SQL data source.
TSQL:
, case when PastDue = 1 then 'BlueStyle' else 'NormalStyle' end as ConditionalFormatting

Open in new window


Style in head section or site theme css:
<style>.BlueStyle{background-color:blue;} .NormalStyle{background-color:white;}</style>

Open in new window


Then in your Template Field control definition:
CssClass='<%# Eval("ConditionalFormatting")%>'

Open in new window

Respectfully yours,


Alan
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 39988115
You could do that, but you don't really want to be adding markup logic to the database end of things. It makes more sense, and will keep things more neatly separated, if you leave decisions about visuals to your WebForm.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now