Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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!

  • *Experts*
Posted

For the record, you can use a column in a where clause without including it in the select. So you could do:

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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

Jarod
Posted

This is not working for me:

 

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

  • Moderators
Posted

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.

ds.Tables(0).Columns(1).ColumnMapping = MappingType.Hidden
'or
ds.Tables("myTable").Columns("myColumn").ColumnMapping = MappingType.Hidden

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...