Link to home
Start Free TrialLog in
Avatar of phoenixfire425
phoenixfire425Flag for United States of America

asked on

Conditional Formatting on DataGrid and Graphs. asp.net

I have a page that i am working on and i am trying to do a couple of things that i cannot seem to get to work.

First is Conditional Formatting.
I have researched and tried to test a variety of solutions to have a value get highlighted if below a value.
i was using this script in my code behind and i cannot get this to work on the page.

__________
Sub ItemDataBoundEventHandler(sender as Object, e as DataGridItemEventArgs)
   If e.Item.ItemType = ListItemType.Item OR _
      e.Item.ItemType = ListItemType.AlternatingItem then
      'Check to see if the price is below a certain threshold
      Dim price as Double
      price = Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "price"))
     
      If price < 10.0 then
          e.Item.BackColor = System.Drawing.Color.Yellow
      End If
   End If
End Sub
_________________

Second i would like also to have some type of graph on the page to for the datagrid. And I cannot find anything that even gets close to working for me on this.  And if there is a possible way can i have a java screen show this on a mouse over?

Lastly is there a way to have the ASP.Net page calculate a value for a column instead of having SQL do this?

Any Help would be great.
Thank You
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Hi,

Hit this link ,where it has shoen how you can change cell's back color as per condition

http://www.codeproject.com/KB/webforms/gridcolumnformatting.aspx

Thanks
Harshad
Avatar of phoenixfire425

ASKER

i actually just tried that. and i get an error after converting this script to VB. stating that a variable was expected.

Avatar of Dustin Hopkins
AS for the script in your question I plugged it into a datagrid and it worked fine. you may want to inspect other parts of the code, datagrid values and such. And you can have a calculated column like if you send the dataitems to a function. ie below
'basic calculation
<asp:TemplateField HeaderText="Total">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("OASGN")+Eval("WASGN")+Eval("EASGN") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
'more advanced should be sent to a backend function
            <asp:TemplateField HeaderText="Percent">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# findpercent((Eval("OASGN")+Eval("WASGN")+ Eval("EASGN")) , Eval("TAUTH")) %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
'backend function
Public Function FindPercent(ByVal total As Double, ByVal auth As Double)
        Dim percent As Double = total / auth
        Dim spercent As String = percent.ToString("P")
        Return (spercent)
    End Function

Open in new window

Also is your datagrid tied to the function? if not you may want to add the "handles" directive or make sure the function is tied in.
This may sound dumb but i cannot get the handles function to work.
i am not 100% sure if i have the GridView1 tied.

how should i check this?

And i will try the Calulation in the DataGrid Soon as i get the conditional formating done.
Also do you know of anyway to graph data from the DataGrid?

wait are you wanting to use conditional formatting on a DataGrid or a GridView? for the gridview the code is a little different
 Sub gv_RowDatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            
            If Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "price")) < 10.0 Then
                e.Row.BackColor = Drawing.Color.Yellow
                e.Row.ForeColor = Drawing.Color.Black
            End If
 
        End If
    End Sub

Open in new window

Well, if you're not using handles the you would need to look at your gridview declaration...you should have the sub specified ie.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="gv_RowDataBound" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
Wow. That sort of worked the way i wanted..
Is there a way for it to just highligh just the column that the row is in ? instead of the entire row?

i guess now i have a bigger question.
What is the main difference between the DataGrid scripting and the Gridview?
ASKER CERTIFIED SOLUTION
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
since poodles, rings of fire, and small childeren for sacifice are kinda high in demand except poodles but not many want them anyways. haha i am glad i am not taking that route.
i have to use the gridview because of the sorting, paging, and exporting functions.

the code you sent i am assuming that the (1) is the row number. starting at the 0 count and up?

now what about the graphs? is there a simple way of doing this?
now for the calulation i am having a hard time with this
if i wanted to take column "FEB" and divide it by "JAN" would the code go
how do you tell the code to Divide?

<asp:TemplateField HeaderText="Total">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("FEB")+Eval("JAN")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
Yes the 1 is the column number starting with 0.
As for the graphing, no there isn't really an easy way to do this as you will need to draw the picture. I believe there are a few open source controls for doing this. I can't think of any, but google is a good friend.

Ok for division, it tends to error if you use it inline, I've had better luck sending it to a function.
<asp:Label ID="Label2" runat="server" Text='<%# Dividestuff(Eval("FEB"),Eval("JAN") %>'></asp:Label>
 
Public Function Dividestuff(ByVal number1 As Double, ByVal number2 As Double)
        Dim answer As Double = number1 / number2
        Return (answer)
    End Function
                

Open in new window

OOUCH!

got this fun error.
what does this mean that i should do?


Parser Error 
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 
 
Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:Label' is of type 'System.Web.UI.WebControls.Label'.
 
Source Error: 
 
 
Line 60:                         DataFormatString="{0:c}" HeaderText="Dec 07" HtmlEncode="False" 
Line 61:                         SortExpression="Dec 07" />
Line 62:                         <asp:Label ID="Label2" runat="server" Text='<%# Dividestuff(Eval("FEB"),Eval("JAN") %>'></asp:Label>
Line 63:                     <asp:BoundField ApplyFormatInEditMode="True" DataField="Jan" DataFormatString="{0:c}"
Line 64:                         HeaderText="Jan" ReadOnly="True" SortExpression="Jan">
 

Open in new window

the label needs to be in a template control
<asp:TemplateField HeaderText="Percent">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Dividestuff(Eval("FEB"),Eval("JAN") %>'></asp:Label>                </ItemTemplate>
            </asp:TemplateField>

Open in new window

Sorry that was my error

But now i am getting this error.


Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
 
Compiler Error Message: BC30057: Too many arguments to 'Public Function Dividestuff(number1 As Double, number2 As Double) As Object'.
 
Source Error:
 
 
 
Line 62:                     <asp:TemplateField HeaderText="Percent">
Line 63:                          <ItemTemplate>
Line 64:                         <asp:Label ID="Label2" runat="server" Text='<%# Dividestuff(Eval("FEB"),Eval("JAN") %>'></asp:Label>                </ItemTemplate>
Line 65:                           </asp:TemplateField>
Line 66:                     <asp:BoundField ApplyFormatInEditMode="True" DataField="Jan" DataFormatString="{0:c}"
 
 
Source File: D:\Repcom\template\Reports\CustomerSalesForYearByMonthWithREPID.aspx    Line: 64 

Open in new window

i got the value working but the only problem is that it displays alot in infinite values and alot o NaN values.
Also i cannot sort using the "Percent" as a sort expression.

would it be better to do the calculation on the SQL side?
As for the infinites you can remedy the situation by checking that the divisor is not 0. If your wanting to sort, then yes it would be better to do this one the sqlside.