Validate numbers by: TWO DECIMAL PLACES & THOUSANDS DIVIDED BY COMMAS

Hi all, I have never done this type of coding and looked a bit on google but did not actually find what I was looking for.  Could you please supply me with a code snippet of what I am looking for?

I want code, that I could validate numerical values in a datagrid, validating all numbers in the datagrid with two decimal values and all thousands with commas, like the example below.

Current Value = 1211111.2323222
ValidatedValue = 1,211,111.23

Thanks, all help is highly appreciated.
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.

samtran0331Commented:
Mezillinu,

It sounds like you're looking to format a string, not validate it.
Have a look at this:
http://msconline.maconstate.edu/tutorials/ASPNET2/ASPNET07/aspnet07-01.aspx
Scroll through the article and look for the section: "Selecting and Formatting Columns"
and the "Data Formatting" section also.
0
MezillinuAuthor Commented:

ok this is it. Thanks :-)

But what I actually had in mind, is -

a function that i give it a column name, and for example it does something like this pseudo code.

for each row in datagrid.rows
row.Text = String.Format("{0:N}", 12345.678)
next

do you understand? Is it possible? because i have no idea how to do it.

0
samtran0331Commented:
before i go any further,
are you using a datagrid (asp.net 1.1x) or a gridview (asp.net 2.0)?

and yes it is possible, but the answer is slightly different depending on whether you're using a datagrid or a gridview.

also, is this column a template column or a bound column?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

samtran0331Commented:
also, c# or vb.net?
0
MezillinuAuthor Commented:
I am using asp.net 2, with bound columns.

And using vb.net.

Thank you for your attention
0
samtran0331Commented:
With VB.Net and a Gridview, you can format the column data in the code using the grid's rowdatabound event.
Below is a working example, keep in mind that the:
  e.Row.Cells(0).Text

the zero is the column number you want to format.

ignore the page_load, it's just to make some fake data
aspx:
=================================================
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
 
 
code:
=================================================
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            'fake data to give some rows
            Dim dt As New DataTable
            Dim col As New DataColumn("MyColumn")
            dt.Columns.Add(col)
            For i As Integer = 0 To 9
                Dim r As DataRow
                r = dt.NewRow
                r.Item("MyColumn") = ((System.Math.Pow(i, 5) * 3.14159265) + (i * i * 0.00034)) * 100
                dt.Rows.Add(r)
            Next
 
            'bind to datagrid
            With Me.GridView1
                .DataSource = dt
                .DataBind()
            End With
        End If
    End Sub
 
    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'The next 2 lines are how to format:
            Dim num As Decimal = CType(e.Row.Cells(0).Text, Decimal)
            e.Row.Cells(0).Text = FormatNumber(num, 2, True, False, True)
        End If
    End Sub

Open in new window

0
MezillinuAuthor Commented:
so this formats the numbers in 2 decimal places, and divides thousands by commmas?
0
samtran0331Commented:
Yes
0
MezillinuAuthor Commented:
If e.Row.RowType = DataControlRowType.DataRow Then
            'The next 2 lines are how to format:
            Dim num As Decimal = CType(e.Row.Cells(0).Text, Decimal)
            e.Row.Cells(0).Text = FormatNumber(num, 2, True, False, True)
        End If

If I may ask, so I will know actually what i am doing, what are you doing here? May you please explain a little?
0
MezillinuAuthor Commented:
How can I make a function that I give it the grid view, and it will search and format all the columns if numbers are available?

This is what I actually had in mind :-) Sorry for bothering you though :-(
You are very helpful!
0
samtran0331Commented:
using the formatnumber function:
http://msconline.maconstate.edu/tutorials/VBNET/VBNET01/vbnet01-08.aspx

below is the code commented line by line
    'rowdatabound loops through the grid rows
    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.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 e.Row.Cells.Count - 1 'loop through each cell in the row
                If IsNumeric(e.Row.Cells(i).Text) = True Then 'see if the value is a number
                    num = CType(e.Row.Cells(i).Text, Decimal) 'if it is a number, assign to variable
                    e.Row.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
    End Sub

Open in new window

0
MezillinuAuthor Commented:
Is it possible for a function like this below ?

Is it possible for a function like this below ?
 
Public Sub FormatColumn(HERE I GIVE IT THE GRIDVIEW TO FORMAT)
     If e.Row.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 e.Row.Cells.Count - 1 'loop through each cell in the row
                If IsNumeric(e.Row.Cells(i).Text) = True Then 'see if the value is a number
                    num = CType(e.Row.Cells(i).Text, Decimal) 'if it is a number, assign to variable
                    e.Row.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
End Sub

Open in new window

0
samtran0331Commented:

    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

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
MezillinuAuthor Commented:
great dude!!
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.