vb.net datatype in manually added column
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>开发者_如何学C;
</html>
and nothing happend to my data
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>
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.
精彩评论