Datagrid - Couple Questions

Disasterpiece

Freshman
Joined
Apr 2, 2003
Messages
47
Location
Blacksburg, VA
I've been posting quite a bit here today...anyway here's my issue:

With the datagrid control, I have 2 questions:

1. Is there a way to hide a column from displaying in the datagrid? I use a select statement that's based off of that column but I don't want it to show up in the output for the datagrid.

2. Is there a way to format columns in the datagrid? I'm using an Access Database - I have 2 fields that are set to Type Currency, however in my program's Datagrid they are just showing up as Decimal, and if there it's something like 154.00, it simply shows up as 154. I want it to display as $154.00 in the data grid.

Any way to do this?
 
Check out the DataGridTableStyle object. You can exclude columns through that (a grid has a default one, I think, but I can't remember the property name that exposes it).

When you create a new DataGridTextBoxColumn object, you can specify it's Format property. Something like "c" should give you currency in the local machine format.

-Nerseus
 
Cool thanks a bunch.

Everyone here has been a big help so far, I'll try your suggestions after I eat dinner.

As for why I use that column in the Select - I have a table filled with transactions based on usernames. I also have a table of usernames and other information. When a user logs into the program, the select statement gets the rows for only that username, however I don't want to actually display the username in the datagrid.

Thanks again!
 
For the record, you can use a column in a where clause without including it in the select. So you could do:
Code:
SELECT FirstName, LastName FROM Users WHERE UserName = 'dan'

But, sometimes you need extra columns (especially for joining tables that are stored separately in the DataSet and used for DataRelations) and then you just have to hide them.

And at least you provided some code, Robby. I was too lazy to clean up my own sample code :)

-Nerseus
 
To hide a column you must use a DatGridTableStyle and set a DataGridtextBoxColumn.width to 0
However, if you try that, you will see some drawbacks when tabbing the datagrid. You should listen to the CurrentCellChanged event to check if the current cell is a 0-width one and so reset it to the correct one
You can find everything or so at
http://www.syncfusion.com/FAQ/WinForms/default.asp#44
 
Or you can.....
C#:
ds.Tables["myTable"].Columns["SomeColumn"].ColumnMapping = MappingType.Hidden
 
Last edited:
This is not working for me:

Visual Basic:
Dim dgStyle As New DataGridTableStyle()
        With dgStyle
            .AlternatingBackColor = Color.LightGray
            .BackColor = Color.WhiteSmoke
            .ForeColor = Color.MidnightBlue
            .GridLineColor = Color.Honeydew
            .GridLineStyle = System.Windows.Forms.DataGridLineStyle.Solid
            .HeaderBackColor = Color.MidnightBlue
            .HeaderFont = New Font("Arial", 8.0!, FontStyle.Bold)
            .HeaderForeColor = Color.White
            .LinkColor = Color.Teal
            .MappingName = "Appointments"
            .SelectionBackColor = Color.Yellow
            .SelectionForeColor = Color.DarkMagenta
        End With


        Dim grd5 As New DataGridTextBoxColumn()
        With grd5
            .HeaderText = "Description"
            .MappingName = "Description"
            .Width = 75
        End With

        Dim grd6 As New DataGridTextBoxColumn()
        With grd6
            .HeaderText = "Dur"
            .MappingName = "Duration"
            .Alignment = HorizontalAlignment.Center
            .Width = 30
            .Format = "c" ' <<< This will set the column to currency
        End With

        dgStyle.GridColumnStyles.AddRange(New DataGridColumnStyle() {grd5, grd6})
        DataGrid1.TableStyles.Add(dgStyle)

I put that in my Form Load event before I link the datagrid with the datasource, doesn't work. Could someone explain what all of these properties are?

Thanks
 
Just to elaborate on hidding a column in a datagrid, this line of code should appear after you fill the DataAdapter and before you implement any TableStyles.
Visual Basic:
ds.Tables(0).Columns(1).ColumnMapping = MappingType.Hidden
'or
ds.Tables("myTable").Columns("myColumn").ColumnMapping = MappingType.Hidden
 
Back
Top