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
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
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
the visibility of the scrollbar is caused by
RowIndicatorVisibility = Visibility.Visible
I would like to keep the RowIndicator. How can I handle it?
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;
}
}
}));
}
}
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;
}));
}
Regards,
Stefan Nenchev
Telerik by Progress
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