Solved

Gridview and conditional formatting...

Posted on 2014-04-07
6
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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