Initial Header Column Width

4 posts, 2 answers
  1. Jose Simas
    Jose Simas avatar
    35 posts
    Member since:
    Mar 2009

    Posted 19 Apr 2011 Link to this post

    Hi,

    I have a GridView bound to a DataTable. When the data is loaded each column appears to be sized to the width of the name of the column. Is it possible to change this behaviour so that the width of the column is determined by the widest data item (the value in the cells) of that column instead?

    Cheers,
    Jose
  2. Answer
    Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Apr 2011 Link to this post

    Hi Jose,

     
    You are right, by default the ColumnWidth property of RadGridView is set to SizeToHeader, it will size the cells according to the column header and SizeToCells will size the cells according to the cells content.
    You may change it as shown below:

    this.radgridView.ColumnWidth = GridViewLength.SizeToCells;

    Kind regards,
    Vanya Pavlova
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Jose Simas
    Jose Simas avatar
    35 posts
    Member since:
    Mar 2009

    Posted 19 Apr 2011 Link to this post

    Hi Vanya,

    Thanks for that. I applied the SizeToCells to my grid successfully. But I also need to align the cells to the right and give them a minimum width which I am doing by applying a style:

    <Style TargetType="{x:Type telerik:GridViewCell}">
        <Style.Setters>
            <Setter Property="MinWidth" Value="65"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
        </Style.Setters>
    </Style>

    But when I added the MinWidth setter to the style the HorizontalAlignment setter no longer works and my text is all aligned to the left. Is there a different way to apply a MinWidth for the columns in conjuction with the SizeToCells? Am I doing something wrong here?

    Thanks,
    Jose 
  5. Answer
    Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Apr 2011 Link to this post

    Hello Jose,


    Due to the template structure of GridViewCell, If you need to specify the alignment of the cell within a style use the HorizontalContentAlignment/VerticalContentAlignment properties instead:

    <Style TargetType="{x:Type telerik:GridViewCell}">
        <Style.Setters>
            <Setter Property="MinWidth" Value="65"/>
            <Setter Property="HorizontalContentAlignment" Value="Right"/>
        </Style.Setters>
    </Style>


    Furthermore you may just specify the TextAlignment property of a column in RadGridView.


    Kind regards,
    Vanya Pavlova
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top