Link to home
Start Free TrialLog in
Avatar of Mezillinu
MezillinuFlag for Malta

asked on

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.
Avatar of samtran0331
samtran0331
Flag of United States of America image

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.
Avatar of Mezillinu

ASKER


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.

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?
also, c# or vb.net?
I am using asp.net 2, with bound columns.

And using vb.net.

Thank you for your attention
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

so this formats the numbers in 2 decimal places, and divides thousands by commmas?
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?
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!
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

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

ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
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
great dude!!