Solved

Format DataGrid Output Format

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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