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?
 
RejojohnyConnect With a Mentor Commented:
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
 
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
All Courses

From novice to tech pro — start learning today.