This is a migrated thread and some comments may be shown as answers.

Make Column Header Invisible

6 Answers 195 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Evan
Top achievements
Rank 1
Evan asked on 28 Nov 2017, 09:04 PM

I am trying to make a DataGrid with only one DataGridTextColumn. I do not want a header for this column, is there a flag or someway of making it invisible?

Thanks,

Evan

6 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Senior Manager Technical Support
Telerik team
answered on 29 Nov 2017, 08:51 PM
Hi Evan,

Currently there isn't a "HeaderVisibility" property for the DataGrid column types (see the properties list here).

However, you can achieve this by using a custom HeaderTemplate in conjunction with a HeaderStyle, try this for example:

<dataGrid:DataGridTextColumn PropertyName="Title">
    <dataGrid:DataGridTextColumn.HeaderStyle>
        <dataGrid:DataGridColumnHeaderStyle BorderColor="Transparent"
                                            BorderThickness="0"
                                            BackgroundColor="Transparent"
                                            TextMargin="0"
                                            ... />
    </dataGrid:DataGridTextColumn.HeaderStyle>
    <dataGrid:DataGridTextColumn.HeaderContentTemplate>
        <DataTemplate>
            <Grid Padding="0"
                  Margin="0" />
        </DataTemplate>
    </dataGrid:DataGridTextColumn.HeaderContentTemplate>
</dataGrid:DataGridTextColumn>


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Evan
Top achievements
Rank 1
answered on 29 Nov 2017, 09:15 PM
Thank you this worked for our needs
0
Michael
Top achievements
Rank 1
answered on 18 Aug 2020, 08:38 PM
This is a good example. But... I am using ExpandoObjects to populate the grid because the number of columns are variable. The above code doesn't work. Any suggestions to hide the headers in this circumstance?
0
Lance | Senior Manager Technical Support
Telerik team
answered on 18 Aug 2020, 08:49 PM

Hello Michael,

All you need to do this is access to the column object (it doesn't have to be in XAML).

If you are creating the columns on the fly, then you can do this:

myColumn.HeaderTemplate = GenerateDataTemplateSeenInPreviousReply();
myColumn.HeaderStyle = new DataGridColumnHeaderStyle() { use properties in my last reply };

If you are using AutoGenerateColumns=True, then this get a little trickier because there isn't an OnColumnGenerating event. You will need to decide when the time is right to iterate over all the columns and set the same values.

foreach(var column in MyDataGrid.Columns)
{
    column.HeaderTemplate = ...;
    column.HeaderStyle = ...;
}

 

Regards,
Lance | Manager - Technical Support
Progress Telerik

0
Michael
Top achievements
Rank 1
answered on 18 Aug 2020, 09:45 PM
Thank you for the super fast response! Is there a way to do that without code behind? I'm using MVVM and would like to keep the code behind as small as possible.
0
Lance | Senior Manager Technical Support
Telerik team
answered on 18 Aug 2020, 10:55 PM

Hello Michael,

Not really. A few ideas I have that you could look into:

  • Write a custom behavior that subscribes to the DataBindingComplete event (and access the column sin the behavior)
  • Use EventToCommandBehavior with DatabindingComplete event and pass the DataGrid object to the view model (via CommandParameter)
  • (unverified) Use OneWayToSource binding for the DataGrid Columns collection. You could then iterate over the Columns in the view model directly.

The bottom line is you need access to the Columns, so you can iterate over them to set their HeaderTemplate and HeaderStyle properties.

Regards,
Lance | Manager - Technical Support
Progress Telerik

Tags
DataGrid
Asked by
Evan
Top achievements
Rank 1
Answers by
Lance | Senior Manager Technical Support
Telerik team
Evan
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Share this question
or