[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1292
  • Last Modified:

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.
0
Mezillinu
Asked:
Mezillinu
  • 7
  • 7
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now