Solved

Gridview and conditional formatting...

Posted on 2014-04-07
6
277 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

12 Experts available now in Live!

Get 1:1 Help Now