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

Initial column width

7 Answers 330 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raul
Top achievements
Rank 1
Raul asked on 22 Feb 2017, 02:48 PM

Hello,

I need something like InitialWidth="*". I would use it to set the initial width of some columns to "*". After displaying the gridview I would like that it behaves like if I had set Width="300" (or an other fix value).

The reason is that all customers don't like if I set the with to "*" because it is very difficult the resize the columns to the desired widths (because changing the width of one column affects the other columns). I have some gridviews implemented as UserControls and they are nested in other controls. Thats why I can't set fix width values without enabling the horizontal scrollbar or having some empty unused space at the right side.

It would be ideal if I could set an InitialWidth to fill the complete area and after that the user can resize all columns and enable the horizontal scrollbar if he likes to do it.

 

Best Regards,

Raul

7 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 27 Feb 2017, 09:33 AM
Hello Raul,

You can iterate over the columns collection of the RadGridView once it is loaded and set a fixed width for each column which will be equal to the width calculated by the ColumnWidth="*" property:

private void clubsGrid_Loaded(object sender, RoutedEventArgs e)
       {
          Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (Action)(() =>
          {
              foreach (var col in clubsGrid.Columns)
              {
                  (col as GridViewDataColumn).Width = (col as GridViewDataColumn).ActualWidth;
              }
          }));
       }

I have attached a sample for your reference.

Regards,
Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Raul
Top achievements
Rank 1
answered on 27 Feb 2017, 02:27 PM

Hello Stefan,

thanks for your help. But when I uncomment the body of clubsGrid_Loaded in your sample project, I'm getting a horizontal scrollbar for the gridview. Same happens also in my code.

Regards,

Raul

0
Raul
Top achievements
Rank 1
answered on 27 Feb 2017, 02:52 PM

the visibility of the scrollbar is caused by 

RowIndicatorVisibility = Visibility.Visible

I would like to keep the RowIndicator. How can I handle it?

0
Raul
Top achievements
Rank 1
answered on 27 Feb 2017, 03:08 PM

ok, I'm now using two events and it is working (do you have a better solution?). But wouldn't it be better to calculate the column width considering the visibility of the row indicator instead of layouting the gridview a second time?

private void gv_Loaded(object sender, RoutedEventArgs e) {
    if (gv.RowIndicatorVisibility == Visibility.Collapsed) {
        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (Action)(() => {
            foreach (var col in gv.Columns) {
                if (col.IsVisible) {
                    col.Width = col.ActualWidth;
                }
            }
        }));
    }
}
 
private void gv_SizeChanged(object sender, SizeChangedEventArgs e) {
    if (gv.RowIndicatorVisibility != Visibility.Collapsed) {
        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (Action)(() => {
            foreach (var col in gv.Columns) {
                if (col.IsVisible) {
                    col.Width = col.ActualWidth;
                }
            }
        }));
    }
}
0
Raul
Top achievements
Rank 1
answered on 27 Feb 2017, 05:43 PM
sorry, forget the solution with the second event. It doesn't work. Still looking for a working solution :(
0
Stefan Nenchev
Telerik team
answered on 01 Mar 2017, 11:44 AM
Hi Raul,

You can slightly increase the width of the Window after setting the Width of the columns. The following works in the sample project:

private void clubsGrid_Loaded(object sender, RoutedEventArgs e)
       {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (Action)(() =>
          {
              foreach (var col in clubsGrid.Columns)
              {
                  (col as GridViewDataColumn).Width = (col as GridViewDataColumn).ActualWidth;
              }
              this.Width += 25;
          }));
       }
Would it work for you?

Regards,
Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Raul
Top achievements
Rank 1
answered on 01 Mar 2017, 06:00 PM

Hello Stefan,

I have used another workaround.

I added a dummy column with Width="29"

<telerik:GridViewColumn x:Name="tmp_column" Width="29"/>

 

In code behind I'm setting this column to invisible after assigning the width values to the other columns. That way the width of the RowIndicator is compensated.

private void gv_Loaded(object sender, RoutedEventArgs e) {
    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, (Action)(() => {
        foreach (var col in gv.Columns) {
            col.Width = col.ActualWidth;
        }
        tmp_column.IsVisible = false;
    }));
}

 

Regards,
Raul

Tags
GridView
Asked by
Raul
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Raul
Top achievements
Rank 1
Share this question
or