0
Dim pctofpax As New DataColumn
        pctofpax = New DataColumn("PCTPAX1", GetType(Decimal))
        pctofpax.Expression = "[ASOFPAX] / [YEPAX]"
        ds.Tables("workplease").Columns.Add(pctofpax)

        Dim avgppax As New DataColumn
        avgppax = New DataColumn("AVG PAX", GetType(Double))
        avgppax.Expression = "[Current Sales] / [Current PAX]"
        ds.Tables("workplease").Columns.Add(avgppax)

These are two columns that i added into my asp.net/vb.net datagrid. the problem is i keep trying to change the datatypes so they show up as numbers with only two decimals but it is not working

            <span lang="en-us">Sales As Of Analysis</span><br />
        <asp:GridView ID="GridView1" runat="server" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            GridLines="Vertical">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
    <asp:BoundColumn DataField="PCTPAX1" 
                 HeaderText="PCTPAX1" 
                 DataFormatString="{0:c}">

    </div>
    </form>
</body>
</html>

and nothing happend to my data

2
  • Define "not working." How is it failing? Is it simply a matter of formatting the values in the display to show only two decimals? Or is there some other issue? Commented Nov 30, 2010 at 17:03
  • well i tried doing a decimal(10,2) and that isn't working maybe i am just doing it wrong Commented Nov 30, 2010 at 17:06

2 Answers 2

2

If it's just a matter of displaying only two decimals, this can be accomplished in the display code for the DataGrid:

<asp:BoundField DataField="PCTPAX1" 
                 HeaderText="PCTPAX1" 
                 DataFormatString="{0:c}">

Note the DataFormatString value of {0:c} which specifies that the formatting of the value in that field should be "currency." (You can also try with a capital C, I don't have a test handy for it right now.) There's a lot more you can do with format strings, more information here.

Or do you want the actual values to be rounded to currency values? You'll likely use Math.Round for that, though I don't have an example handy. There's information about that here.

Edit: Based on your comment(s), it sounds like you were attempting to change data types at the database level. This won't really impact the code. The value coming back from the database in the form of a double or decimal or some other numeric likely won't carry that precision back to the code. As far as the DataGrid is concerned, it's calling .ToString() on a double and showing the result. No database data type is involved. Using the DataFormatString property to pass a format string to the .ToString() method is how to change the display.

Edit again based on your question edit: Your HTML is broken. You have the BoundColumn outside of the grid, and the column tag itself isn't closed. Columns go inside the grid. See the grid layout here. Basically, you'd want something more like:

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
              BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
              GridLines="Vertical">
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:BoundField DataField="PCTPAX1" 
                         HeaderText="PCTPAX1" 
                         DataFormatString="{0:c}">
        </asp:BoundField>
    </Columns>
</asp:GridView>
Sign up to request clarification or add additional context in comments.

14 Comments

is there a way i can do it for the entire table i never want to see more then 2 decimals
@NEWprogrammer: Not that I know of. Each column is its own data type and its own data. How many columns are we talking about? Adding DataFormatString to each one shouldn't be a big deal.
yeah i dont care how the values are written in the database, because i don't plan on allowing an update from my program to the database, the main concern is viewing purposes. However, it looks really messy with 0000 everywhere
Ah, you're using a GridView and it looks like the BoundColumn control doesn't work with that. Try BoundField instead. I'll update my answer.
for me as long as i am learning, the badges and points don't mean much. the knowledge is enough, and when it comes to asp.net and sql i lack a lot of it.
|
0

Changing the type of a column which is already in your datatable doesn't work. or at least not that i know. if you just want to get a different formatting, look at the answers above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.