How do I make the GridViewDataColumn narrower?

4 posts, 1 answers
  1. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 02 Apr 2013 Link to this post

    I've got a GridView control on a window, that has some columns in it. One of the columns is wider than we need it to be. The values in that column are nearly always single digits. (There's only 4 records out of tens of thousands, in our database, which is either 10 or 11, and 11 is the highest any of our records go.) Anyway, in looking at the properties of the GridViewDataColumn I was that the MinWidth was 20. I set it to 3, but that didn't help. In fact, it didn't even look as though I'd changed the width at all. So, how do I get it to be narrower than it currently is displayed at? (The column I'm trying to narrow is the one associated with the CaseNumber data.) Here's the XAML:

    <telerik:RadGridView VerticalAlignment="Stretch" AutoGenerateColumns="False" DataMember="ClientNumber"
                         x:Name="grdClients" d:DataContext="{d:DesignData Source=/SampleData/ASISelectClientSampleCollection.xaml}"
                         IsReadOnly="True" ShowColumnHeaders="False" ShowGroupPanel="False"
                         RowIndicatorVisibility="Collapsed"
                         IsFilteringAllowed="False" AutoExpandGroups="True" FontSize="16" SelectionChanged="grdClients_SelectionChanged"
                         ItemsSource="{Binding}" HorizontalAlignment="Left" MouseDoubleClick="grdClients_MouseDoubleClick"
                         >
      <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn UniqueName="ClientNumber">
          <telerik:GridViewColumn.ToolTipTemplate>
            <DataTemplate>
              <TextBlock Text="Client Number" />
            </DataTemplate>
          </telerik:GridViewColumn.ToolTipTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn UniqueName="LastName">
          <telerik:GridViewColumn.ToolTipTemplate>
            <DataTemplate>
              <TextBlock Text="Last Name" />
            </DataTemplate>
          </telerik:GridViewColumn.ToolTipTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn UniqueName="FirstNames">
          <telerik:GridViewColumn.ToolTipTemplate>
            <DataTemplate>
              <TextBlock Text="First Name" />
            </DataTemplate>
          </telerik:GridViewColumn.ToolTipTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn UniqueName="CaseNumber">
          <telerik:GridViewColumn.ToolTipTemplate>
            <DataTemplate>
              <TextBlock Text="Case Number" />
            </DataTemplate>
          </telerik:GridViewColumn.ToolTipTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn UniqueName="DateOfBirth" DataFormatString="{}{0:d}">
          <telerik:GridViewColumn.ToolTipTemplate>
            <DataTemplate>
              <TextBlock Text="DOB" />
            </DataTemplate>
          </telerik:GridViewColumn.ToolTipTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn UniqueName="InterviewDate" DataFormatString="{}{0:d}">
          <telerik:GridViewColumn.ToolTipTemplate>
            <DataTemplate>
              <TextBlock Text="Interview Date" />
            </DataTemplate>
          </telerik:GridViewColumn.ToolTipTemplate>
        </telerik:GridViewDataColumn>
      </telerik:RadGridView.Columns>
      <telerik:RadGridView.SortDescriptors>
        <telerik:SortDescriptor Member="LastName" SortDirection="Ascending" />
        <telerik:SortDescriptor Member="FirstNames" SortDirection="Ascending" />
        <telerik:SortDescriptor Member="CaseNumber" SortDirection="Descending" />
      </telerik:RadGridView.SortDescriptors>
      <telerik:RadGridView.GroupDescriptors>
        <telerik:GroupDescriptor Member="ClientNumber">
        </telerik:GroupDescriptor>
      </telerik:RadGridView.GroupDescriptors>
      <telerik:RadGridView.GroupHeaderTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Horizontal" MinWidth="180">
              <TextBlock Text="{Binding Group.Items[0].LastName}" />
              <TextBlock Text=", " />
              <TextBlock Text="{Binding Group.Items[0].FirstNames}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding Group.Items[0].SocialSecurityNo}" FontWeight="Bold"  />
            </StackPanel>
          </StackPanel>
        </DataTemplate>
      </telerik:RadGridView.GroupHeaderTemplate>
    </telerik:RadGridView>



  2. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 03 Apr 2013 Link to this post

    Hello,

    You could set Width="SizeToCells" for the column. That way its width will be adjusted to the cell's content. 
     

    Greetings,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Rod
    Rod avatar
    71 posts
    Member since:
    Jan 2011

    Posted 03 Apr 2013 Link to this post

    Thank you, Didie, I'll give that a try. One thing I don't understand, though, is that when I first get into VS 2012 and open the solution, the column is already at the correct size. It's only after I run it for the first time, that it expands to the size it remains for the rest of the development session. Why is that? (I haven't yet implemented the Width="SizeToCells" feature that you suggested.)
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 04 Apr 2013 Link to this post

    Hello,

    If you set a MaxWidth value for the column, then it will be respected in case its content needs more place to be shown. MinWidth means that this is the minimum Size the cell can be narrowed to.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top