Make Column Header Invisible

7 posts, 1 answers
  1. Evan
    Evan avatar
    13 posts
    Member since:
    Sep 2017

    Posted 28 Nov 2017 Link to this post

    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

  2. Answer
    Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1185 posts

    Posted 29 Nov 2017 Link to this post

    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
  3. Evan
    Evan avatar
    13 posts
    Member since:
    Sep 2017

    Posted 29 Nov 2017 in reply to Lance | Manager Technical Support Link to this post

    Thank you this worked for our needs
  4. Michael
    Michael avatar
    2 posts
    Member since:
    Mar 2020

    Posted 18 Aug 2020 in reply to Lance | Manager Technical Support Link to this post

    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?
  5. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1185 posts

    Posted 18 Aug 2020 Link to this post

    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

  6. Michael
    Michael avatar
    2 posts
    Member since:
    Mar 2020

    Posted 18 Aug 2020 in reply to Lance | Manager Technical Support Link to this post

    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.
  7. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1185 posts

    Posted 18 Aug 2020 Link to this post

    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

Back to Top