Completed
Last Updated: 07 Dec 2016 13:49 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 25 Aug 2016 06:32
Category: GridView
Type: Bug Report
0
FIX. RadGridView - ArgumentOutOfRangeException when best-fitting the columns after regenerating the columns in a grouped grid
To reproduce:

DataTable dt = new DataTable();

public Form1()
{
    InitializeComponent();
 
    for (int i = 0; i < 5; i++)
    {
        dt.Columns.Add("Col" + i);
    }
     
    for (int i = 0; i < 5; i++)
    {
        DataRow row = dt.NewRow();
        dt.Rows.Add(row);
        foreach (DataColumn col in dt.Columns)
        {
            row[col.ColumnName] = randomWord(2, 14);
        }
    }
}

static Random r = new Random();
static string chars = "AEIOUBCDFGHJKLMNPQRSTVWXYZ";

static string randomWord(int minlen, int maxlen)
{
    double d1 = minlen + r.NextDouble() * (maxlen - minlen); 
    
    int len = (int)d1;
    
    char[] word = new char[len];
    for (int i = 0; i < len; ++i)
    {
        int index = ((int)Math.Round(25 * r.NextDouble() + 0.4999999999));
        word[i] = chars[index];
    }
    return new string(word);
}

public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }

    public Item(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}

private void radButton1_Click(object sender, EventArgs e)
{
    this.radGridView1.Columns.Clear();
    foreach (DataColumn col in dt.Columns)
    {
        this.radGridView1.Columns.Add(col.ColumnName);
        this.radGridView1.Columns.Last().FieldName = col.ColumnName;
    }
     this.radGridView1.BestFitColumns(BestFitColumnMode.AllCells); 
}

private void Form1_Load(object sender, EventArgs e)
{
    this.radGridView1.AutoGenerateColumns = false;
    this.radGridView1.DataSource = dt; 

    foreach (DataColumn col in dt.Columns)
    {
        this.radGridView1.Columns.Add(col.ColumnName); 
        this.radGridView1.Columns.Last().FieldName = col.ColumnName;
    }
     this.radGridView1.BestFitColumns(BestFitColumnMode.AllCells); 
}

Workaround: clear the GroupDescriptors  collection as well and add the GroupDescriptor programmatically: http://docs.telerik.com/devtools/winforms/gridview/grouping/setting-groups-programmatically
0 comments