Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGrid Formating for Headers and Columns

Posted on 2004-10-24
8
Medium Priority
?
220 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
Comment
Question by:Tygh
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 12397703
>>"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
ID: 12397710
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:
RonaldBiemans earned 2000 total points
ID: 12398178
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Tygh
ID: 12400459
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
ID: 12400476
Nevermind got it
0
 

Author Comment

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

Expert Comment

by:RonaldBiemans
ID: 12400886
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
ID: 12400995
YEP!!!

Thanks a lot!!!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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