Solved

Format DataGrid Output Format

Posted on 2003-11-10
4
5,238 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
  • 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 99 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now