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

Problem with GridViewColumnGroup

2 Answers 119 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nikita
Top achievements
Rank 1
Nikita asked on 03 Jul 2014, 06:19 AM
Hi everyone!
We are doing a project, and use it in GridView and its capabilities. We had a problem with GridViewColumnGroup. Grid is used to display services for a week, and have the button to show / not show weekend. By default, the “show weekend” is disabled. When we click the button “show weekend”, the weekend columns for some reason added small, and not as the same width as the weekdays. But if you do show weekend the default, they appear normal. But if you then show \ not show the weekend, they are becoming less and less. 

public partial class Form1 : Form
{
    private static readonly List<string> DaysOfWorkingWeekList = new List<string>(new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"});
    private static readonly List<string> DaysOfWeekList = new List<string>(new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" });
 
    public Form1()
    {
        InitializeComponent();
 
        foreach (var column in GetColumns())
        {
            rgvMain.MasterTemplate.Columns.Add(column);
        }
        rgvMain.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
        UpdateGroupHeader();
 
        var serviceWeeks = new List<ServiceWeek>();
        var serviceWeek = new ServiceWeek();
        serviceWeeks.Add(serviceWeek);
 
        rgvMain.DataSource = serviceWeeks;
    }
 
    private void UpdateGroupHeader()
    {
        AddGroupHeader(rgvMain.MasterTemplate, radCheckBox1.IsChecked ? DaysOfWeekList : DaysOfWorkingWeekList);
        rgvMain.MasterTemplate.Refresh();
    }
 
    private void radCheckBox1_CheckStateChanged(object sender, EventArgs e)
    {
        UpdateGroupHeader();
    }
     
    public void AddGroupHeader(GridViewTemplate template, List<string> groupHeaderText)
    {
        var columnGroupsView = new ColumnGroupsViewDefinition();
 
        for (int i = 0, index = 0; index < groupHeaderText.Count; index++)
        {
            columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup(groupHeaderText[index]));
            columnGroupsView.ColumnGroups[index].Rows.Add(new GridViewColumnGroupRow());
 
            columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(template.Columns[i++]);
            columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(template.Columns[i++]);
        }
 
        template.ViewDefinition = columnGroupsView;
    }
 
    private IEnumerable<GridViewTextBoxColumn> GetColumns()
    {
        return new List<GridViewTextBoxColumn>
        {
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50, MaxWidth = 50}
        };
    }
}
 
public class Service
{
}
 
public class ServiceWeek
{
    public Service MondayService { get; set; }
    public Service SundayService { get; set; }
    public Service SaturdayService { get; set; }
    public Service FridayService { get; set; }
    public Service ThursdayService { get; set; }
    public Service WednesdayService { get; set; }
    public Service TuesdayService { get; set; }
}


Please help me! On the form located only a standard RadGridView and CheckBox.
Sorry for my English.

2 Answers, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 07 Jul 2014, 04:32 PM
Hi Nikita,

Thank you for contacting us.

This happens because the AutoSizeColumnMode is set to Fill and the width of the columns and the groups is recalculated so that they fill the available space. However, due to floating point precision errors, the width of the last columns, which you hide and show, decreases every time. This is a known flaw in the ColumnGroupsViewDefinition and we will try to improve this in the future.

The best way to overcome this is to disable the Fill mode while you are populating the columns. Additionally, setting MaxWidth to the columns when they are in Fill mode might have some undesired effects and therefore I have removed this setting from the GetColumns method.
private void UpdateGroupHeader()
{
    rgvMain.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;
 
    AddGroupHeader(rgvMain.MasterTemplate, radCheckBox1.IsChecked ? DaysOfWeekList : DaysOfWorkingWeekList);
    rgvMain.MasterTemplate.Refresh();
 
    rgvMain.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
}
 
public void AddGroupHeader(GridViewTemplate template, List<string> groupHeaderText)
{
    var columnGroupsView = new ColumnGroupsViewDefinition();
 
    for (int i = 0, index = 0; index < groupHeaderText.Count; index++)
    {
        columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup(groupHeaderText[index]));
        columnGroupsView.ColumnGroups[index].Rows.Add(new GridViewColumnGroupRow());
 
        columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(template.Columns[i++]);
        columnGroupsView.ColumnGroups[index].Rows[0].Columns[0].Width = 100;
        columnGroupsView.ColumnGroups[index].Rows[0].Columns.Add(template.Columns[i++]);
        columnGroupsView.ColumnGroups[index].Rows[0].Columns[1].Width = 100;
    }
 
    template.ViewDefinition = columnGroupsView;
}
 
private IEnumerable<GridViewTextBoxColumn> GetColumns()
{
    return new List<GridViewTextBoxColumn>
        {
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50},
            new GridViewTextBoxColumn(),
            new GridViewTextBoxColumn {Width = 50, MinWidth = 50}
        };
}

I hope this will help. Feel free to write back if you have any further questions.

Regards,
Ivan Todorov
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Nikita
Top achievements
Rank 1
answered on 08 Jul 2014, 06:01 AM
Hello, Ivan!
Thanks for solution, they helped. Hope this problem will be solved in future releases.
Tags
GridView
Asked by
Nikita
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Nikita
Top achievements
Rank 1
Share this question
or