Displaying only selected columns in 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
 
Yes,

Set the AutoGenerateColumns to false

<asp:DataGrid id="ItemsGrid"
AutoGenerateColumns="false" ...>

You must also specify with columns you want to see.
You do this with templates.

For example:

<DataGrid .....>
<Columns>
<asp:TemplateColumn>
<HeaderStyle></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval
(Container, "DataItem.MyColumnName") %>'
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</DataGrid>

/Fredrik Normén NSQUARED2
 
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