Hi,
I'm using the gridview to show items that are defined like following code:
Because the list of years and the list of months are hierarchically and have variable lengths, the columns for these items must be generated on data load of the datagrid. The bindings are also defined in this part.
The desired behavior of the year column and the corresponding month columns is like this:
I'm using this code to obtain this:
Now, all this works fine, except for the showing and hiding of the columns. The performance of this action is quite slow. when a dozen columns must be hidden (IsVisible=false) and a dozen hidden columns must be shown (IsVisible=true), it takes some seconds.
Can anyone explain to me how this showing and hiding using the IsVisible property works and if there are ways to reduce the rendering time of these actions?
I've added a screenshot of the real life project to this thread. The project is much more complex then the code samples above because it uses converters, custom celltemplates, custom celledittemplates... when creating the columns for month and year. This probably also slows down the rendering.
Best regards,
Pieter Jan Verfaillie
I'm using the gridview to show items that are defined like following code:
public class GridLine
{
public string Key { get; set; }
public List<
Year
> Years { get; set; }
}
public class Year
{
public string Code { get; set; }
public int TotalValue { get { return Months != null ? Months.Sum(m => m.Value) : 0; } }
public List<
Month
> Months { get; set; }
}
public class Month
{
public string Code { get; set; }
public int Value { get; set; }
}
Because the list of years and the list of months are hierarchically and have variable lengths, the columns for these items must be generated on data load of the datagrid. The bindings are also defined in this part.
The desired behavior of the year column and the corresponding month columns is like this:
- when the year column is visible, the month columns aren't visible
- when doubleclicked on the header of the year column, the month columns are shown and the year column is hidden.
- vice versa...
I'm using this code to obtain this:
private void TestGridDataLoaded(object sender, EventArgs e)
{
var items = (List<
GridLine
>)TestGrid.ItemsSource;
if (items == null || items.Count == 0) return;
// Generate columns for hierarchical years and months.
for (int yearCounter = 0; yearCounter <
FixedNumberOfYears
; yearCounter++)
{
var
year
=
items
[0].Years[yearCounter];
var
yearColumn
=
new
GridViewDataColumn
{
Tag
=
year
.Code,
DataMemberBinding
=
new
Binding(string.Format("Years[{0}].TotalValue", yearCounter)),
};
yearColumn.Header
=
CreateColumnHeaderLabel
(yearColumn, year.Code); // creates a double clickable header to toggle visibility
_gridColumns.Add(yearColumn, new List<GridViewDataColumn>());
TestGrid.Columns.Add(yearColumn);
for (var monthCounter = 0; monthCounter < FixedNumberOfMonths; monthCounter++)
{
var month = year.Months[monthCounter];
var monthColumn = new GridViewDataColumn
{
IsVisible = false,
Header = CreateColumnHeaderLabel(yearColumn, month.Code, true),
DataMemberBinding = new Binding(string.Format("Years[{0}].Months[{1}].Value", yearCounter, monthCounter))
};
_gridColumns[yearColumn].Add(monthColumn);
TestGrid.Columns.Add(monthColumn);
}
}
}
private System.Windows.Controls.Label CreateColumnHeaderLabel(GridViewDataColumn yearColumn, string content, bool isMonth = false)
{
var label = new System.Windows.Controls.Label
{
Content = content,
Tag = yearColumn, Foreground = new SolidColorBrush( isMonth ? Colors.Yellow : Colors.White)
};
label.MouseDoubleClick += ToggleColumnsVisibility;
return label;
}
Now, all this works fine, except for the showing and hiding of the columns. The performance of this action is quite slow. when a dozen columns must be hidden (IsVisible=false) and a dozen hidden columns must be shown (IsVisible=true), it takes some seconds.
Can anyone explain to me how this showing and hiding using the IsVisible property works and if there are ways to reduce the rendering time of these actions?
I've added a screenshot of the real life project to this thread. The project is much more complex then the code samples above because it uses converters, custom celltemplates, custom celledittemplates... when creating the columns for month and year. This probably also slows down the rendering.
Best regards,
Pieter Jan Verfaillie