Link to home
Start Free TrialLog in
Avatar of Inteliscape
InteliscapeFlag for Cyprus

asked on

hide column in datagrid

I have this code behind on pageload :

       Dim dss As New DataSet
     
        dss = myProfile.getsubjects(categoryid)

        datagrid1.DataSource = dss.Tables(0)

my dss has 5 columns but i want to show only 2 of them on datagrid. I don't  want to cut the 3 of them as i use all column info.
Just want to show only the 2 columns.

How i change the column name to be diferent from the name of the sql tables i have on my database?

Thanx
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Q1. How do I hide a column in a DataGrid or DataTable?

Ans. DataGrid.Columns(index).Visible = False

See also: Mapping type?

Set DataColumn.MappingType as MappingType.Hidden

ONE MORE VERSION: GRID ONLY:

The grid part of a DataGrid is represented by a table. The TableStyles property provides to this table. TableStyles property returns an object of type DataGridTableStyle, which provides access to all columns in the form of DataGridColumnStyles collection. The GridColumnStyles property of DataGridTableStyle returns it, from where you can get one DataGridColumnStyle. The DataGridColumnStyle represents the style of a column and you need to set Width property of DataGridColumnStyle to 0.

Something like this:

For Each dgt As DataGridTableStyle In myDataGrid.TableStyles
Console.WriteLine(dgt.MappingName)
For Each dgc As DataGridColumnStyle In dgt.GridColumnStyles
dgc.Width = 0
Next dgc
Next dgt

Source: http://www.vbdotnetheaven.com/Uploadfile/prvn_131971/HideDataGridcolumnInVB11172005020153AM/HideDataGridcolumnInVB.aspx?ArticleID=daa8d4d5-7b0d-4265-9f8e-ed62aaabb58e
Avatar of Inteliscape

ASKER

DataGrid.Columns(index).Visible = False

When i write the above i get this message :
'Columns' is not a member of 'System.Windows.Forms.Datagrid'
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry for not mentioning before but i am working on a windows form and not a web one. The code you've sent me before works well in the case of a web application but not a windows one.
Can you please help me on the environment i am working at?