Datagrid + Generic List + Hide Columns
Hi all,
I have the following situation:
A WM5/PPC application with a datagrid that have a generic list of object as datasource. The list of Object is actually a List(of Product) where Product class is a value object with some classic properties, prduct id, code, name etc...
I want to hide some of the fields being displayed and attempted to do so by hiding datagrid columns using DataGridTableStyle. I converted some code from C# and tried to use:
dim ts as new DataGridTableStyle
ts.GridColumnStyles.AddColumn(...)
But it would seem that AddColumn is not a member of GridColumnStyles and it only has the Add method which takes a DataGridColumnStyle as parameter. So i'm a bit :confused: here, is there any way to achieve this hiding of columns?
Re: Datagrid + Generic List + Hide Columns
Re: Datagrid + Generic List + Hide Columns
But I am unable to declare an object as DataGridColumnStyle... This class does not seem to be available, also as mentionned in my first post, GridColumnStyles does not have AddColumn as a member but Add only...
Re: Datagrid + Generic List + Hide Columns
Use System.Windows.Forms.DataGridColumnStyle
Re: Datagrid + Generic List + Hide Columns
Dim locationCol1 As New DataGridTextBoxColumn
Dim locationCol2 As New DataGridTextBoxColumn
.....
Dim aGridTableStyle2 As New DataGridTableStyle
aGridTableStyle2.MappingName = "Source"
.....
With locationCol1
.MappingName = "Reg No"
.Width = 70
.NullText = ""
End With
With locationCol2
.MappingName = "Status"
.Width = 0
.NullText = ""
End With
....
With aGridTableStyle2.GridColumnStyles
.Add(locationCol1)
.Add(locationCol2)
End With
....
dgPermits.TableStyles.Add(aGridTableStyle2)
Should help
Re: Datagrid + Generic List + Hide Columns
Hmm actually i found that using width=-1 actually hide the column, width=0 didn't do it right.
Re: Datagrid + Generic List + Hide Columns
Does not seem to work, on thing I forgot to mention is that I have a generic List(Of Object) as datasource. Each object is in fact a simple Product Class object. From the code you pasted petevick the compiler does not complain, but then i cannot see the column header text as per your code. Any ideas?
Code as below:
Code:
Cursor.Current = Cursors.WaitCursor
Dim objList As List(Of Object) = objProductBO.GetProduct()
Cursor.Current = Cursors.Default
Dim locationCol1 As New DataGridTextBoxColumn
Dim locationCol2 As New DataGridTextBoxColumn
Dim aGridTableStyle2 As New DataGridTableStyle
aGridTableStyle2.MappingName = "Source"
With locationCol1
.MappingName = "Reg No"
.Width = 70
.NullText = ""
End With
With locationCol2
.MappingName = "Status"
.Width = 0
.NullText = ""
End With
With aGridTableStyle2.GridColumnStyles
.Add(locationCol1)
.Add(locationCol2)
End With
dtgProduct.TableStyles.Clear()
dtgProduct.TableStyles.Add(aGridTableStyle2)
If Not objList Is Nothing Then
dtgProduct.DataSource = objList
End If
Whether I assign the datasource before or after the column formating code does not make any difference
Re: Datagrid + Generic List + Hide Columns
Hi,
I seem to remember that code doesn't show headings on the grid. Take a look at the 'headertext' property (I think :))
Re: Datagrid + Generic List + Hide Columns
Still does not work... Could it be that the DataGridTextBoxColumn object is unable to map property name of an object with the MappingName when a List of Object is bound to a datagrid?
Re: Datagrid + Generic List + Hide Columns
Here's a sample code I am trying to test:
Employee Class:
Code:
Public Class Employee
Private empId As Integer
Public Property EmployeeId() As Integer
Get
Return empId
End Get
Set(ByVal value As Integer)
empId = value
End Set
End Property
Private empCode As String
Public Property EmployeeCode() As String
Get
Return empCode
End Get
Set(ByVal value As String)
empCode = value
End Set
End Property
Private empName As String
Public Property EmployeeName() As String
Get
Return empName
End Get
Set(ByVal value As String)
empName = value
End Set
End Property
End Class
Form Code:
Code:
Private empList As New List(Of Employee)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim locationCol1 As New DataGridTextBoxColumn
Dim locationCol2 As New DataGridTextBoxColumn
Dim aGridTableStyle2 As New DataGridTableStyle
aGridTableStyle2.MappingName = "Source"
With locationCol1
.MappingName = "EmployeeId"
.Width = -1
.NullText = ""
End With
With aGridTableStyle2.GridColumnStyles
.Add(locationCol1)
End With
For index As Integer = 1 To 10
Dim emp As New Employee
With emp
.EmployeeId = index
.EmployeeCode = String.Concat("EM", index.ToString)
.EmployeeName = String.Concat("Test Employee ", index.ToString)
End With
empList.Add(emp)
Next
DataGrid1.DataSource = empList
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(aGridTableStyle2)
End Sub
I suspect that the property name is not the same as the Mapping Name. Any suggestions?
Re: Datagrid + Generic List + Hide Columns
Couldn't test that code atm but could you try:
aGridTableStyle2.MappingName = empList.toString()
Re: Datagrid + Generic List + Hide Columns
Here's the code i'm using for the same purpose as you...
Note that i have 2 custom classes called DataGridColoredTextBoxColumn and DataGridCustomTextBoxColumn, you might want to ignore that.
vb Code:
Private Sub formataGrelhaEntidades()
Try
Dim aGridTableStyle2 As New DataGridTableStyle
Dim locationCol1 As New DataGridColoredTextBoxColumn
Dim locationCol2 As New DataGridCustomTextBoxColumn
Dim locationCol3 As New DataGridTextBoxColumn
Dim locationCol4 As New DataGridTextBoxColumn
Dim locationCol5 As New DataGridTextBoxColumn
aGridTableStyle2.MappingName = myDataset.ToString
Me.DataGridEntidades.RowHeadersVisible = False
Me.DataGridEntidades.ColumnHeadersVisible = True
With locationCol1
.backgroundColor = Color.LightSteelBlue
.ForeColor = Color.Black
.MappingName = "Cod"
.HeaderText = "Cod"
.Width = 44
.NullText = ""
End With
With locationCol2
.Owner = Me.DataGridEntidades
.Alignment = HorizontalAlignment.Left
.AlternatingBackColor = Color.LightSteelBlue
.MappingName = "Nome"
.HeaderText = "Empresa"
.ReadOnly = True
.Width = Screen.PrimaryScreen.WorkingArea.Width - locationCol1.Width - 16
.NullText = ""
End With
With locationCol3
.MappingName = "Morada"
.HeaderText = "Morada"
.Width = -1
.NullText = ""
End With
With locationCol4
.MappingName = "CodPostal"
.HeaderText = "CodPostal"
.Width = -1
.NullText = ""
End With
With locationCol5
.MappingName = "Contacto"
.HeaderText = "Contacto"
.Width = -1
.NullText = ""
End With
With aGridTableStyle2.GridColumnStyles
.Add(locationCol1)
.Add(locationCol2)
.Add(locationCol3)
.Add(locationCol4)
.Add(locationCol5)
End With
With DataGridEntidades
.BackColor = Color.AliceBlue
.BackgroundColor = Color.AliceBlue
.GridLineColor = Color.RoyalBlue
.HeaderBackColor = Color.SteelBlue
.SelectionBackColor = Color.LightCoral
.SelectionForeColor = Color.DarkBlue
End With
DataGridEntidades.TableStyles.Clear()
DataGridEntidades.TableStyles.Add(aGridTableStyle2)
Catch ex As Exception
'
End Try
End Sub
Re: Datagrid + Generic List + Hide Columns
aGridTableStyle2.MappingName = empList.toString() does not do much... :(
Re: Datagrid + Generic List + Hide Columns
Have you ever try to use datasets? They use to help, they are fast reading data too.