Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Gridview and conditional formatting...

Posted on 2014-04-07
6
Medium Priority
?
288 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 34

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 800 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 1200 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 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 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