DataGrid Formating for Headers and Columns

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?
TyghAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RonaldBiemansConnect With a Mentor Commented:
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
 
flavoCommented:
>>"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
 
flavoCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
TyghAuthor Commented:
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
 
TyghAuthor Commented:
Nevermind got it
0
 
TyghAuthor Commented:
One thing though, the right align is not working
0
 
RonaldBiemansCommented:
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
 
TyghAuthor Commented:
YEP!!!

Thanks a lot!!!
0
All Courses

From novice to tech pro — start learning today.