Displaying only selected columns in a data grid?

  • Thread starter Thread starter What-a-Tool
  • Start date Start date
W

What-a-Tool

Have an Access table loaded into a DataTable that was created at run time,
and am displaying these contents in a data grid.
Me.dgGames.DataSource = Me.DsGameInfo.Tables("Games")

I would like to display only a selected few of the columns, rather than the
whole table.
Is this possible?
Thanks - Sean
 
Hi What-a-tool,
Have an Access table loaded into a DataTable that was created at run time,
and am displaying these contents in a data grid.
Me.dgGames.DataSource = Me.DsGameInfo.Tables("Games")
I would like to display only a selected few of the columns, rather than the
whole table.

Take a look for "datarow", I think that will explain it for you.
(It is one or more rows from your table that has columns).

I hope this brings you on the route for the solution.
Cor
 
Hi,

If we are speaking of the System.Windows.Forms.DataGrid class, this is
definitely possible.
In the Windows Forms designer, click on the grid and choose the TableStyles
property in the Properties window. Then click on the small button with
ellipsis (...) to the right of the property name to bring up the Table
Styles Collection Editor. Add a new item and set its MappingName to "Games".
Then, go to the property grid for the newly created table style and choose
the GridColumnStyles property. Click on the similar button with ellipsis
(...) to bring up the Grid Column Styles Collection Editor. Add a number of
columns you want and specify header name and mapping name for each column.
Mapping names for the columns should correspond to the names of data columns
in the data table.
 
What-a-Tool said:
Have an Access table loaded into a DataTable that was created at run
time, and am displaying these contents in a data grid.
Me.dgGames.DataSource = Me.DsGameInfo.Tables("Games")

I would like to display only a selected few of the columns, rather
than the whole table.
Is this possible?
Thanks - Sean

This is a question about the Datagrid and not specific to the VB.NET
language. Please turn to one of the
microsoft.public.dotnet.framework.windowsforms groups.
 
Thanks again -guys. Your the best. I'd be one frustrated aspiring programmer
if it wasn't for this group (still am frustrated most of the time - but not
quite as frustrated as I would be!).
Thanks-Sean
 
Or try this

Dim dgtsAdj As DataGridTableStyle

Private Sub SetDataGrid()
Try
Dim sFilter As String
sFilter = "Code = '" & tbCode.Text.Trim & "'"
dvViewAdj.RowFilter = sFilter

dgtsAdj = New DataGridTableStyle
dgtsAdj.MappingName = "Adjustments"

With dgActiveAdj
.DataSource = dvViewAdj
.AllowSorting = True
End With

dgActiveAdj.TableStyles.Clear()
dgActiveAdj.TableStyles.Add(dgtsAdj)
dgActiveAdj.TableStyles(0).ReadOnly = True

With dgtsAdj
.GridColumnStyles(0).Width = 0 'MAKES THEM HIDDEN
.GridColumnStyles(1).Width = 0
.GridColumnStyles(2).Width = 150
.GridColumnStyles(2).HeaderText = "Adjustment Description"
.GridColumnStyles(3).Width = 150
.GridColumnStyles(3).HeaderText = "Adjustment Amount"
.GridColumnStyles(4).Width = 85
.GridColumnStyles(4).HeaderText = "Begin Date"
.GridColumnStyles(5).Width = 85
.GridColumnStyles(5).HeaderText = "End Date"
End With
Catch ex As Exception
MessageBox.Show(ex.Source.ToString & " - " & ex.Message,
Application.ProductName, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub
 
Ended up using the following to hide unwanted columns and set the width of a
column. Now I want to re-arrange the displayed order of the columns, so I
tried creating another table style and adding the columns in the order I
wanted them, but for some reason I end up with all table style settings
gone. Anyone see what I'm doing wrong?
Thanks - Sean

'Hide un-wanted column
Me.DsGameInfo.Tables("Games").Columns("Home").ColumnMapping =
MappingType.Hidden

'Set column widths

Dim dgts As New DataGridTableStyle()

dgts.MappingName = "Games"

Me.dgGames.TableStyles.Add(dgts)

dgts.GridColumnStyles("Game").Width = 150

'Re-arrange colums

Dim dgts2 As New DataGridTableStyle()

dgts2.GridColumnStyles.Add(dgts.GridColumnStyles("Game"))

dgts2.GridColumnStyles.Add(dgts.GridColumnStyles("Home Score"))

dgts2.GridColumnStyles.Add(dgts.GridColumnStyles("Guest Score"))

dgts2.GridColumnStyles.Add(dgts.GridColumnStyles("Week / Year"))

Me.dgGames.TableStyles.Remove(dgts)

Me.dgGames.TableStyles.Add(dgts2)

Me.dgGames.Refresh()
 
Back
Top