Solved

Format DataGrid Output Format

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

17 Experts available now in Live!

Get 1:1 Help Now