Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format DataGrid Output Format

Posted on 2003-11-10
4
Medium Priority
?
5,259 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

773 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