Converting asp.net grid view formatting to datagrid formatting

I have this function that excepts a dataview, then it goes through all its columns searches if they are numeric, and if numeric divides the thousands by commas and 2 decimal places.

I want this function to accept a DATAGRID not a DATAVIEW.

Thanks, any help is greatfully appreciated.
Private Sub FormatGridColumns(ByVal gv As GridView)
        For j As Integer = 0 To gv.Rows.Count - 1 'loop through all rows
            If gv.Rows(j).RowType = DataControlRowType.DataRow Then 'Make sure it's not the header/footer of the grid
                Dim num As Decimal = 0.0 'a variable to hold the data
                For i As Integer = 0 To gv.Rows(j).Cells.Count - 1 'loop through each cell in the row
                    If IsNumeric(gv.Rows(j).Cells(i).Text) = True Then 'see if the value is a number
                        num = CType(gv.Rows(j).Cells(i).Text, Decimal) 'if it is a number, assign to variable
                        gv.Rows(j).Cells(i).Text = FormatNumber(num, 2, True, False, True) 'format the variable and write the text back to the cell
                    End If
                Next
            End If
        Next
    End Sub

Open in new window

LVL 3
MezillinuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RejojohnyCommented:
Private Sub FormatGridColumns(ByVal gv As Datagrid)
        For j As Integer = 0 To gv.Rows.Count - 1 'loop through all rows
           if gv.Rows(j).Item.ItemType = ListItemType.AlternatingItem or
               gv.Rows(j).Item.ItemType = ListItemType.Item then 'Make sure it's not the header/footer of the grid
                Dim num As Decimal = 0.0 'a variable to hold the data
                For i As Integer = 0 To gv.Rows(j).Cells.Count - 1 'loop through each cell in the row
                    If IsNumeric(gv.Rows(j).Cells(i).Text) = True Then 'see if the value is a number
                        num = CType(gv.Rows(j).Cells(i).Text, Decimal) 'if it is a number, assign to variable
                        gv.Rows(j).Cells(i).Text = FormatNumber(num, 2, True, False, True) 'format the variable and write the text back to the cell
                    End If
                Next
            End If
        Next
    End Sub
 

try this ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RejojohnyCommented:
a better option would be to set the dataformatstring property of the databound column in the gridview .. something like dataformatstring = {0.f2}

that is it .. no need of loop or function to call ..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.