Completed
Last Updated: 13 Oct 2015 10:05 by ADMIN
ADMIN
Hristo
Created on: 06 Oct 2015 08:00
Category: GridView
Type: Bug Report
0
FIX. RadGridView - Data exception when loading a layout containing column expressions while the grid has expressions defined for the same columns
How to reproduce:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.radGridView1.DataSource = this.GetData();
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
    }
    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(int));
        dt.Columns.Add("Date", typeof(DateTime));
        dt.Columns.Add("Bool", typeof(bool));
        for (int i = 0; i < 100; i++)
        {
            dt.Rows.Add(i, "Name " + i, i + 10, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
        }

        return dt;
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        this.radGridView1.Columns[1].Expression = "Id * Age";
    }

    private void radButton2_Click(object sender, EventArgs e)
    {
        string s = "default.xml";
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
        dialog.Title = "Select a xml file";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            s = dialog.FileName;
        }
        this.radGridView1.SaveLayout(s);
    }

    private void radButton3_Click(object sender, EventArgs e)
    {
        string s = "default.xml";
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
        dialog.Title = "Select a xml file";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            s = dialog.FileName;
        }
        this.radGridView1.LoadLayout(s);
    }
}

Workaround: iterate the columns prior to loading the layout and remove the expressions
private void radButton3_Click(object sender, EventArgs e)
{
    foreach (GridViewDataColumn col in this.radGridView1.Columns)
    {
        col.Expression = "";
    }

    string s = "default.xml";
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
    dialog.Title = "Select a xml file";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        s = dialog.FileName;
    }
    this.radGridView1.LoadLayout(s);
}
0 comments