Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Gridview and conditional formatting...

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

664 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