This is a migrated thread and some comments may be shown as answers.

[Solved] Format Grid Row

6 Answers 242 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Fredrick Zilz
Top achievements
Rank 1
Fredrick Zilz asked on 14 Jan 2010, 12:30 AM
I know I have done this before, but I can't seem to get this figured out.  I have a RadGrid and based on which row I want to format the text to {0:C}.  The APX:

 

 

 
 <telerik:RadGrid ID="GridView2" runat="server" HorizontalAlign="Left" Skin="Web20" OnItemDataBound="RadGrid1_ItemDataBound" >                       
                        <MasterTableView> 
                            <RowIndicatorColumn> 
                                <HeaderStyle Width="20px" /> 
                            </RowIndicatorColumn> 
                            <ExpandCollapseColumn> 
                                <HeaderStyle Width="20px" /> 
                            </ExpandCollapseColumn> 
                        </MasterTableView> 
                    </telerik:RadGrid>  

for an asp.net gridview I did this using rowdatabound:

 

 
    Sub gvCurrentYearBudgetnew_rowdatabound(ByVal sender As ObjectByVal e As GridViewRowEventArgs) Handles gvCurrentYearBudgetNew.RowDataBound  
        Dim i As Integer 
        Dim str1 As String = "nothing yet" 
        If e.Row.RowType = DataControlRowType.DataRow Then 
            e.Row.Cells(1).Font.Bold = True 
            e.Row.Cells(5).Font.Bold = True 
            e.Row.Cells(6).Font.Bold = True 
 
 
            e.Row.Cells(1).BackColor = Drawing.Color.Bisque  
            e.Row.Cells(5).BackColor = Drawing.Color.Bisque  
            e.Row.Cells(6).BackColor = Drawing.Color.Bisque  
 
            Select Case RTrim(e.Row.Cells(0).Text)  
                Case "Sales" 
                    For i = 1 To e.Row.Cells.Count - 1  
                        If Not String.IsNullOrEmpty(e.Row.Cells(i).Text) Then 
                            Try 
                                e.Row.Cells(i).Text = String.Format("{0:c}"Double.Parse(e.Row.Cells(i).Text))  
                            Catch 
                            End Try 
                        End If 
                    Next 
                Case "Gross Profit" 
                    For i = 1 To e.Row.Cells.Count - 1  
 
                        If Not String.IsNullOrEmpty(e.Row.Cells(i).Text) Then 
                            Try 
                                e.Row.Cells(i).Text = String.Format("{0:c}"Double.Parse(e.Row.Cells(i).Text))  
                            Catch 
                            End Try 
                        End If 
                    Next 
                Case "Price" 
                    For i = 1 To e.Row.Cells.Count - 1  
                        If Not String.IsNullOrEmpty(e.Row.Cells(i).Text) Then 
                            Try 
                                e.Row.Cells(i).Text = String.Format("{0:c}"Double.Parse(e.Row.Cells(i).Text))  
                            Catch 
                            End Try 
                        End If 
                    Next 
                Case "Actual GP" 
                    For i = 1 To e.Row.Cells.Count - 1  
                        If Not String.IsNullOrEmpty(e.Row.Cells(i).Text) Then 
                            Try 
                                e.Row.Cells(i).Text = String.Format("{0:c}"Double.Parse(e.Row.Cells(i).Text))  
                                e.Row.Cells(i).ForeColor = Drawing.Color.ForestGreen  
                            Catch 
                            End Try 
                        End If 
                    Next 
                Case "Actual Sales" 
                    For i = 1 To e.Row.Cells.Count - 1  
                        If Not String.IsNullOrEmpty(e.Row.Cells(i).Text) Then 
                            Try 
                                e.Row.Cells(i).Text = String.Format("{0:c}"Double.Parse(e.Row.Cells(i).Text))  
                                e.Row.Cells(i).ForeColor = Drawing.Color.ForestGreen  
                            Catch 
                            End Try 
                        End If 
                    Next 
            End Select 
        End If 
    End Sub 
So I just selected the row based on the text in the first column, the for each cell in the row I applied my format.

Can someone help me do the same thing using the Rad Ajax 3.5  Radgrid.   Thank you.

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Jan 2010, 05:49 AM
Hello Fredrick,

You can go through the following document which should help you understand how to format grid cells based on conditions:
Conditional Formatting for rows/cells on ItemDataBound

Also the following document should help you understand how to access cells and rows in a grid:
Accessing cells and rows

Hope these help..
Princy.
0
Fredrick Zilz
Top achievements
Rank 1
answered on 14 Jan 2010, 04:29 PM
I have read that article many times and just don't see how to identify a row and iterate through the cells of the row.   I am sure I am just missing something obvious, once I have identified the griddataitem how do I apply a string format.  I have found examples of applying string fromat to a column using the DataStringFormat property, but Griddataitem does not appear to have this property.

Can you give me an example in VB of how to do this.

If (typeof e.item is griddataitem) then  
dim databounditem as griddataitem = ctype(e.item, griddataitem)  
if databounditem("label").text <> "quantity" then   
databounditem("Column2"). ....  
end if 

Thank you.  also, versus identifying the column by unique name "name" can I iterate through them:
dim i as integer  
for i = 0 to cells.count - 1  
 
next  
 
 

Sorry, kind of lost without datarow and walking through row.cells.

Thank you in advance for your time.
0
Fredrick Zilz
Top achievements
Rank 1
answered on 14 Jan 2010, 04:50 PM
I can change the forecolor for a specific item (identified by  based on the criteria of the first column (which is what I need to do), but I cant figure out the correct way of applying string format to the item. or iterating through the columns for the row.  I will keep digging.

Thanks again.
 Protected Sub gridview2_itemdatabound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs)  
        Dim i As Integer = 0  
 
        If (TypeOf e.Item Is GridDataItem) Then 
            Dim databounditem As GridDataItem = CType(e.Item, GridDataItem)  
            If databounditem("label").Text <> "Quantity" Then 
                databounditem("apr").ForeColor = Drawing.Color.DarkRed  
 
            End If 
        End If 
0
Fredrick Zilz
Top achievements
Rank 1
answered on 14 Jan 2010, 07:02 PM
I tried this, but it does not work (the format is not applied):
Protected Sub gridview2_itemdatabound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs)  
        Dim i As Integer = 0  
 
        If (TypeOf e.Item Is GridDataItem) Then 
            Dim databounditem As GridDataItem = CType(e.Item, GridDataItem)  
            If databounditem("label").Text <> "Quantity" Then 
                databounditem("apr").Text = String.Format("{0:c}", databounditem("apr").Text.ToString)  
            End If 
        End If 
I also tried this:
<telerik:RadGrid ID="GridView2" runat="server" HorizontalAlign="Left"  OnItemDataBound="Gridview2_ItemDataBound" AutoGenerateColumns=false >                       
                         <MasterTableView><Columns>  
                                <Telerik:GridBoundColumn DataField="Label" /> 
                         <%--   <asp:BoundField DataField="YTD" HeaderText="Apr-Dec" HtmlEncode="False" /> --%> 
                                   <Telerik:GridBoundColumn DataField="apr" HeaderText="Apr" DataFormatString="{0:c}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="may" HeaderText="May" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="jun" HeaderText="Jun" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="Qtr1" HeaderText="Qtr1" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                  <Telerik:GridBoundColumn DataField="jul" HeaderText="Jul" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="aug" HeaderText="Aug" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="sep" HeaderText="Sep" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="Qtr2" HeaderText="Qtr2" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                 <Telerik:GridBoundColumn DataField="oct" HeaderText="Oct" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="nov" HeaderText="Nov" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="dec" HeaderText="Dec" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="Qtr3" HeaderText="Qtr3" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="jan" HeaderText="Jan" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="feb" HeaderText="Feb" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="mar" HeaderText="Mar" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="Qtr4" HeaderText="Qtr4" DataFormatString="{0:C}" HtmlEncode="False" /> 
                                <Telerik:GridBoundColumn DataField="Yr" HeaderText="Yr" DataFormatString="{0:C}" HtmlEncode="False" /> 
                            </Columns></MasterTableView>  
                    </telerik:RadGrid>  
 and that does not give me a currency format either.
0
Accepted
Princy
Top achievements
Rank 2
answered on 15 Jan 2010, 09:21 AM
Hello Fredrick,

You can only convert numeric values into currency format. So please check the data type for the various columns in your grid.

-Princy.
0
Fredrick Zilz
Top achievements
Rank 1
answered on 15 Jan 2010, 09:48 PM
Thank you.  That pointed me in the right direction.

This code is working for me.  I could not figure out how to iterate throught the griddataitems, so I created a string array of the column names and iterate through that.

 Protected Sub gvCurrentYearBudget_itemdatabound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs)  
 
        Dim myForeColor As Drawing.Color  
        myForeColor = Drawing.Color.DarkTurquoise  
        Dim q As Integer 
        Dim dArray(12) As String 
 
        For q = 0 To 12  
            dArray(q) = "0" 
        Next 
        dArray(0) = "apr" 
        dArray(1) = "may" 
        dArray(2) = "jun" 
        dArray(3) = "jul" 
        dArray(4) = "aug" 
        dArray(5) = "sep" 
        dArray(6) = "oct" 
        dArray(7) = "nov" 
        dArray(8) = "dec" 
        dArray(9) = "jan" 
        dArray(10) = "feb" 
        dArray(11) = "mar" 
 
        If (TypeOf e.Item Is GridDataItem) Then 
 
            Dim databounditem As GridDataItem = CType(e.Item, GridDataItem)  
            Select Case databounditem("label").Text  
 
                Case "Sales" 
                    For q = 0 To 11  
                        If Not (databounditem(dArray(q)).Text.Trim().Equals("&nbsp;"Or databounditem(dArray(q)).Text.Trim().Length = 0) Then 
                            databounditem(dArray(q)).Text = String.Format("{0:C}"CDbl(databounditem(dArray(q)).Text))  
                        End If 
                    Next 
                     
                Case "Gross Profit" 
                    For q = 0 To 11  
                        If Not (databounditem(dArray(q)).Text.Trim().Equals("&nbsp;"Or databounditem(dArray(q)).Text.Trim().Length = 0) Then 
                            databounditem(dArray(q)).Text = String.Format("{0:C}"CDbl(databounditem(dArray(q)).Text))  
                        End If 
                    Next 
                Case "Price" 
                    For q = 0 To 11  
                        If Not (databounditem(dArray(q)).Text.Trim().Equals("&nbsp;"Or databounditem(dArray(q)).Text.Trim().Length = 0) Then 
                            databounditem(dArray(q)).Text = String.Format("{0:C}"CDbl(databounditem(dArray(q)).Text))  
                        End If 
                    Next 
                Case "Quantity" 
 
                Case "Actual Sales" 
                    databounditem("label").ForeColor = myForeColor  
                    For q = 0 To 11  
                        If Not (databounditem(dArray(q)).Text.Trim().Equals("&nbsp;"Or databounditem(dArray(q)).Text.Trim().Length = 0) Then 
                            databounditem(dArray(q)).Text = String.Format("{0:C}"CDbl(databounditem(dArray(q)).Text))  
                        End If 
                    Next 
                Case "Actual GP" 
                    databounditem("label").ForeColor = myForeColor  
                    For q = 0 To 11  
                        If Not (databounditem(dArray(q)).Text.Trim().Equals("&nbsp;"Or databounditem(dArray(q)).Text.Trim().Length = 0) Then 
                            databounditem(dArray(q)).Text = String.Format("{0:C}"CDbl(databounditem(dArray(q)).Text))  
                        End If 
                    Next 
 
            End Select 
 
 
                  End If 
    End Sub 
 
Thank you again for your assistance
Tags
Grid
Asked by
Fredrick Zilz
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Fredrick Zilz
Top achievements
Rank 1
Share this question
or