Use datagrid to display only few columns

mcerk

Regular
Joined
Nov 1, 2004
Messages
78
Hi. I have populated my dataset and dataadapter with all fields (id, Field1, Field2, Field3, Field4... etc)

If I bound datagrid to this dataset, then all fields are displayed.

So If I want to display only Field1 and Field2, do I have to change my SQL SELECT statement in data adapter?


(I'm looking for some property to edit datagrid columns like in vb6. Is this possible?......)


tx,
matej
 
Yes, You'll have to create a tablestyle, add relevant columns with tablemappings for the fields that you want, then add the tablestyle to your grid.

something like: (vb code)

Dim ts as new DataGridTableStyle
dim col1 as New DataGridTextBoxColumn
col1.MappingName="FIRSTCOL"
col1.HeaderText="First Column"
ts.GridTableStyles.Add(col1)

dim col2 as new DataGridTextBoxcolumn
col2.MappingName="SECONDCOL"
col2.HeaderText = "Second Column"
ts.GridTableStyles.Add(col2)

'(repeat blocks as necessary)

myGrid.TableStyles.Clear()
myGrid.TableStyles.Add(ts)
 
hm... it somehow doesnt work.

I put this code to form_load (after I filled dataset).

Visual Basic:
        'define Datagrid!!!
        Dim ts As New DataGridTableStyle
        Dim col1 As New DataGridTextBoxColumn
        col1.MappingName = "temeljnenaloge.opis"
        col1.HeaderText = "Temeljna naloga"
        ts.GridColumnStyles.Add(col1)

        Dim col2 As New DataGridTextBoxColumn
        col2.MappingName = "sif_standardi.id"
        col2.HeaderText = "Standard 1"
        ts.GridColumnStyles.Add(col2)

        Dim col3 As New DataGridTextBoxColumn
        col3.MappingName = "sif_standardi_1.id"
        col3.HeaderText = "Standard 2"
        ts.GridColumnStyles.Add(col3)

        Dim col4 As New DataGridTextBoxColumn
        col4.MappingName = "sif_standardi_2.id"
        col4.HeaderText = "Standard 2"
        ts.GridColumnStyles.Add(col4)
        '(repeat blocks as necessary)

        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(ts)


Just nothing happens. App runs normally, no error, there are still all columns displayed....


please help
 
It would be interesting to see what the auto-generated column headers for your ID fields are.

AFAIK, the ColumnMapping property shouldn't include the table name. However, You would end up with three identical 'ID' fields in your datatable. I'm guessing that these will be called something like ID, ID1 and ID2.

you will need to set the ColumnMapping property to exactly how these are displayed in your grid currently.

B.
 
OK. I have changed my SQL SELECT clause, so there is no duplicates in field names. (ColumnMapping names are just field names).


But nothing changes. My Datagrid still displays all fields selected in SQL SELECT clause.

Do I need to tell datagrid to actually use this created TableStyle, and how.


tx, matej
 
IF there is an error in your tablestyle it will revert back to default. Thus maksure the mapping names really exist in the dataset.

Also do you realise you have named to columns the same.....
 
Back
Top