Disasterpiece Posted April 22, 2003 Posted April 22, 2003 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? Quote
*Experts* Nerseus Posted April 22, 2003 *Experts* Posted April 22, 2003 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 Quote "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
Moderators Robby Posted April 22, 2003 Moderators Posted April 22, 2003 1. Why is it in the Select part if you don't want to display it? Anyway, you can hide it using the method shown in the link below. 2. http://www.xtremedotnettalk.com/showthread.php?s=&threadid=70325&highlight=currency [edit]D'OH, Nerseus wins again. :( [/edit] Quote Visit...Bassic Software
Disasterpiece Posted April 22, 2003 Author Posted April 22, 2003 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! Quote
*Experts* Nerseus Posted April 22, 2003 *Experts* Posted April 22, 2003 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 Quote "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
Jarod Posted April 23, 2003 Posted April 23, 2003 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 Quote Jarod
Moderators Robby Posted April 23, 2003 Moderators Posted April 23, 2003 (edited) Or you can..... ds.Tables["myTable"].Columns["SomeColumn"].ColumnMapping = MappingType.Hidden Edited April 23, 2003 by Robby Quote Visit...Bassic Software
Disasterpiece Posted April 23, 2003 Author Posted April 23, 2003 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 Quote
Moderators Robby Posted April 23, 2003 Moderators Posted April 23, 2003 You should call the formatting after you have loaded the data. Quote Visit...Bassic Software
Moderators Robby Posted April 23, 2003 Moderators Posted April 23, 2003 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 Quote Visit...Bassic Software
Disasterpiece Posted April 23, 2003 Author Posted April 23, 2003 I've got everything under control now, I did it through the Properties window for the Datagrid instead, thanks. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.