Completed
Last Updated: 07 Sep 2015 07:43 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 03 Jun 2015 11:08
Category: GridView
Type: Bug Report
1
FIX. RadGridView - ColumnGroupsViewDefinition - GridViewColumn.MinWidth is not respected when AutoSizeColumnsMode is set to Fill
To reproduce:

private void Form1_Load(object sender, EventArgs e)
{
    this.customersTableAdapter.Fill(this.nwindDataSet.Customers);

    this.radGridView1.DataSource = this.customersBindingSource;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;

    foreach (GridViewColumn col in this.radGridView1.Columns)
    {
        col.MinWidth = 100;
    }
    
    ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
    view.ColumnGroups.Add(new GridViewColumnGroup("Customer Contact"));
    view.ColumnGroups.Add(new GridViewColumnGroup("Details"));
    view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Address"));
    view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Contact"));
    view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
    view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["CompanyName"]);
    view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactName"]);
    view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactTitle"]);

    view.ColumnGroups[1].Groups[0].Rows.Add(new GridViewColumnGroupRow());
    view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Address"]);
    view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["City"]);
    view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Country"]);

    view.ColumnGroups[1].Groups[1].Rows.Add(new GridViewColumnGroupRow());
    view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Phone"]);
    view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Fax"]);
    radGridView1.ViewDefinition = view;
}

Please refer to the attached gif file illustrating the behavior when no ColumnGroupsViewDefinition is applied. The horizontal scroll-car should appear as well when the grid's width is less than the total MinWidth of all columns in case of ColumnGroupsViewDefinition.

Workaround: use GridViewAutoSizeColumnsMode.None and handle the RadGridView.SizeChanged event where you can adjust manually the Width of the visible columns on a way to simulate GridViewAutoSizeColumnsMode.Fill. Here is a sample approach:

private void Form1_Load(object sender, EventArgs e)
{
    this.customersTableAdapter.Fill(this.nwindDataSet.Customers);

    this.radGridView1.DataSource = this.customersBindingSource;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.None;

    foreach (GridViewColumn col in this.radGridView1.Columns)
    {
        col.MinWidth = 100;
    }

    ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
    view.ColumnGroups.Add(new GridViewColumnGroup("Customer Contact"));
    view.ColumnGroups.Add(new GridViewColumnGroup("Details"));
    view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Address"));
    view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Contact"));
    view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
    view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["CompanyName"]);
    view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactName"]);
    view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactTitle"]);

    view.ColumnGroups[1].Groups[0].Rows.Add(new GridViewColumnGroupRow());
    view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Address"]);
    view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["City"]);
    view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Country"]);

    view.ColumnGroups[1].Groups[1].Rows.Add(new GridViewColumnGroupRow());
    view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Phone"]);
    view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Fax"]);
    radGridView1.ViewDefinition = view;

    radGridView1.SizeChanged += radGridView1_SizeChanged;
}

private void radGridView1_SizeChanged(object sender, EventArgs e)
{
    int totalMinimumWidth = 0;
    int columnsCount = ColumnsCountWidth(out totalMinimumWidth);
    int delta = this.radGridView1.Width - totalMinimumWidth;
    AdjustColumnWidth(delta / columnsCount);
}

private void AdjustColumnWidth(int widthToAdd)
{
    ColumnGroupsViewDefinition view = this.radGridView1.ViewDefinition as ColumnGroupsViewDefinition;
    if (view == null)
    {
        return;
    }

    foreach (GridViewColumnGroup group in view.ColumnGroups)
    {
        AdjustColumnsInGroup(widthToAdd, group);
    }
}

private void AdjustColumnsInGroup(int widthToAdd, GridViewColumnGroup group)
{
    if (group.Groups.Count == 0)
    {
        foreach (GridViewColumnGroupRow row in group.Rows)
        {
            foreach (GridViewColumn col in row.Columns)
            {
                col.Width += widthToAdd;
            }
        }
    }
    else
    {
        foreach (GridViewColumnGroup g in group.Groups)
        {
            AdjustColumnsInGroup(widthToAdd, g);
        }
    }
}

private int ColumnsCountWidth(out int totalMinWidth)
{
    int columnsCount = 0;
    totalMinWidth = 0;
    ColumnGroupsViewDefinition view = this.radGridView1.ViewDefinition as ColumnGroupsViewDefinition;
    if (view == null)
    {
        return this.radGridView1.Columns.Count;
    }

    foreach (GridViewColumnGroup group in view.ColumnGroups)
    {
        columnsCount = IterateGroups(columnsCount, group, ref totalMinWidth);              
    }
    return columnsCount;
}

private static int IterateGroups(int columnsCount, GridViewColumnGroup group, ref int totalMinWidth)
{
    int count = columnsCount;
    if (group.Groups.Count == 0)
    {
        foreach (GridViewColumnGroupRow row in group.Rows)
        {
            count += row.Columns.Count;
            foreach (GridViewColumn col in row.Columns)
            {
                totalMinWidth += col.Width;
            }
        }
    }
    else
    {
        foreach (GridViewColumnGroup g in group.Groups)
        {
            count = IterateGroups(count, g, ref totalMinWidth);
        }
    }
    return count;
}
0 comments