Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format DataGrid Output Format

Posted on 2003-11-10
4
Medium Priority
?
5,253 Views
Last Modified: 2007-12-19
hi,

  i m using a dataset to fill in a datagrid in VB.NET, i wish to know
how to format the output for the datagrid to show all the numeric
field in such a way i want it,which is including the decimal place.

Example i have some data stored at SQL Server as below

Product             Qty                    Unit Price
---------            -----                  ------------
ABC                   1                         2
DEF                   1                         3.5
XYZ                   1                         10


Product = varchar
Qty = float
Unit Price = money

Currently,the datagrid will show as

Product             Qty                    Unit Price
---------            -----                  ------------
ABC                   1                         2
DEF                   1                         3.5
XYZ                   1                         10


How can i do it in such a way the data will show as something like

Product             Qty                    Unit Price
---------            -----                  ------------
ABC                   1.00                    2.00
DEF                   1.00                    3.50
XYZ                   1.00                   10.00


0
Comment
Question by:tayleeming
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 9719503
Give DataFormatString="{0:F2}" in the BoundColumn ...

<asp:DataGrid ID="MyDataGrid" Runat="server" AutoGenerateColumns="False">
      <Columns>
            <asp:BoundColumn DataField="Product" HeaderText="Product"></asp:BoundColumn>
            <asp:BoundColumn DataField="Qty" DataFormatString="{0:F2}" HeaderText="Quantity"></asp:BoundColumn>
            <asp:BoundColumn DataField="UnitPrice" DataFormatString="{0:F2}" HeaderText="Unit Price"></asp:BoundColumn>
      </Columns>
</asp:DataGrid>
0
 

Author Comment

by:tayleeming
ID: 9719767
hi, amit_g

  thanks for the help, but it seem to be the solution for Web Application, I m doing Window Application with Vb.NET, Sorry i didn't state it early...

 i actually use a dataAdapter & dataSet to fill the datagrid, below is my code, please advise  


Public Sub SetInitGrid()
        ' Initiate a new Data Adapter
        Dim dbAdaptr As System.Data.SqlClient.SqlDataAdapter
        Dim SQLStr As String = "Select  item,upc,description,pos_remark,price_original,price,disc_type,disc,qty,amt,remark from t_delivery_d"

        dbAdaptr = New System.Data.SqlClient.SqlDataAdapter(SQLStr, G_sqlDB_CN)

        With dbAdaptr
            ' Populate table
            .TableMappings.Add("Table", "t_delivery_d")
            ds = New DataSet()

            .Fill(ds)
            .Dispose()
        End With

        ' DataGrid settings
        With DataGrid1

            ' Set DataGrid Data Binding to table
            .SetDataBinding(ds, "t_delivery_d")
            .TableStyles.Clear()
            .ReadOnly = True
        End With

        '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        ' Set data grid Table Style
        Dim tblCrrncMngr As CurrencyManager = CType(BindingContext(ds.Tables.Item("t_delivery_d")), CurrencyManager)

        Dim TblStyle As New DataGridTableStyle()
        With TblStyle
            .MappingName = "t_delivery_d"
            .AlternatingBackColor = System.Drawing.Color.Aquamarine
            .RowHeaderWidth = 20

            ' Set column styles
            With .GridColumnStyles
                ' Set datagrid ColumnStyle for id_customer field
                .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("item")))
                With .Item(0)
                    .MappingName = "item"
                    .ReadOnly = True
                    .HeaderText = "Item"
                    .Width = 40
                    .NullText = String.Empty
                    .ReadOnly = True
                End With

                .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("UPC")))
                With .Item(1)
                    .MappingName = "UPC"
                    .ReadOnly = True
                    .HeaderText = "UPC"
                    .Width = 100
                    .NullText = String.Empty
                    .ReadOnly = True
                End With

                .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("description")))
                With .Item(2)
                    .MappingName = "description"
                    .ReadOnly = True
                    .HeaderText = "Primary Description"
                    .Width = 260
                    .NullText = String.Empty
                    .ReadOnly = True
                End With


                .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("price_original")))
                With .Item(3)
                    .MappingName = "price_original"
                    .Alignment = HorizontalAlignment.Right
                    .ReadOnly = True
                    .HeaderText = "Original Price "
                    .Width = 80
                    .NullText = "0.00"
                    .ReadOnly = True
                End With


        ' Add TableStyle
        DataGrid1.TableStyles.Add(TblStyle)

    End Sub
0
 

Accepted Solution

by:
Pea913 earned 396 total points
ID: 9720691
try using .Format = "c" inside your with .Item(3) end with

            With .Item(3)
                    .MappingName = "price_original"
                    .Alignment = HorizontalAlignment.Right
                    .ReadOnly = True
                    .HeaderText = "Original Price "
                    .Width = 80
                    .NullText = "0.00"
                    .ReadOnly = True
                    .Format = "c"
                End With

If you want to change the currency info just include the formatinfo property. For your reference look for DataGridTextBoxColumn.Format.

I have tried it and it is working.

0
 

Author Comment

by:tayleeming
ID: 9727420
hi, i manage to solve this issue by this

.Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("price_original"),"0.00"))


anyway thanks for the idea....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question