Solved

DataGrid Formating for Headers and Columns

Posted on 2004-10-24
189 Views
Last Modified: 2010-04-24
Hello out there.

I am new to .Net and I am trying to use the datagrid to show some data from my access DB.  Here are the following columns that I want to display.

STOCKNUM Text
DESC Text
ASKINGPRICE Currency

I want to be able to fill the data grid with these fields but I want to change the column headers to more user friendly names (Stock #, Description, Asking Price)

and also I want the last column (Asking Price) to be formatted in currency and aligned to the right.

here is my relevant code:

Private dvInventory As DataView
    Private mydaInventory As OleDbDataAdapter
    Dim ds As New DataSet


   With cmdSelect
            .CommandText = "Select  StockNum, Desc, AskingPrice From Inventory"
            .Connection = cnn
        End With

        mydaInventory = New OleDbDataAdapter
        mydaInventory.SelectCommand = cmdSelect

        cnn.Open()

        mydaInventory.Fill(ds)
        cnn.Close()

    dvInventory = ds.Tables(0).DefaultView

        With grdInventory
            .DataSource = dvInventory
            .AllowNavigation = False
        End With


How do get this to look the way I want?
0
Question by:Tygh
    8 Comments
     
    LVL 34

    Expert Comment

    by:flavo
    >>"Select  StockNum, Desc, AskingPrice From Inventory"

    Change it to

    select stockNum as [Stock #], Desc as [Description], Format(AskingPrice,"currency")  as [Asking Price] from Inventory

    Dave!
    0
     
    LVL 34

    Expert Comment

    by:flavo
    oh sorry, its not going to like the " around currency

    "select stockNum as [Stock #], Desc as [Description], Format(AskingPrice,'currency')  as [Asking Price] from Inventory"

    Dave
    0
     
    LVL 25

    Accepted Solution

    by:
    or you could use the datagridcolumn styles, something like

    Dim ts As DataGridTableStyle
            ts = New DataGridTableStyle
            ts.MappingName = ds.Tables(0).ToString()

            Dim col1 As New DataGridTextBoxColumn
            With col1
                .MappingName = "STOCKNUM"
                .HeaderText = "Stock #"
                .Width = 80
            End With

            Dim col2 As New DataGridTextBoxColumn
            With col2
                .MappingName = "DESC"
                .HeaderText = "Description"
                .Width = 100
            End With

            Dim col3 As New DataGridTextBoxColumn
            With col3
                .MappingName = "ASKINGPRICE"
                .HeaderText = "Asking Price"
                .Width = 100
                .Format = "c"
                .TextBox.TextAlign = HorizontalAlignment.Right
            End With

            ts.GridColumnStyles.AddRange(New DataGridColumnStyle() {col1, col2, col3})
            DataGrid1.TableStyles.Add(ts)
    0
     

    Author Comment

    by:Tygh
    I am attempting to do the datgrid column styles as shown above.  Is there another step?  It currently displays nothing in the datagrid.  I am wondering if I still have to bind the grid somehow.
    0
     

    Author Comment

    by:Tygh
    Nevermind got it
    0
     

    Author Comment

    by:Tygh
    One thing though, the right align is not working
    0
     
    LVL 25

    Expert Comment

    by:RonaldBiemans
    maybe try this instead

    Dim col3 As New DataGridTextBoxColumn
            With col3
                .MappingName = "ASKINGPRICE"
                .HeaderText = "Asking Price"
                .Width = 100
                .Format = "c"
                .Alignment = HorizontalAlignment.Right
            End With
    0
     

    Author Comment

    by:Tygh
    YEP!!!

    Thanks a lot!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Title # Comments Views Activity
    .Net void multi-threading 6 20
    vb.net / trigger function 3 37
    Reset progress bar in vb.net 2 23
    Linq Unique list ? 3 18
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    This video discusses moving either the default database or any database to a new volume.

    877 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

    20 Experts available now in Live!

    Get 1:1 Help Now