Hello Jay,
Thank you for writing.
When the RadGridView saves its layout, it stores the currently available columns into a xml. The load layout functionality restores
all of the serialized columns into the grid. It is desired and correct behavior of the introduced functionality.
If the initial data source has columns A, B, C,
D and you save these columns in the xml, then change the data source, which contains columns A, B, C,
E. Loading the layout will restore columns A, B, C,
D. This is by design. However, if you want to manipulate the restored columns, it is necessary to be handled manually: remove the redundant columns and add the missing columns. Here is a sample code snippet, demonstrating the described scenario above. If the initial DataTable contains columns A, B, C, D, save the layout, rebind to the second data source with columns A, B, C, E and load the layout again. Then remove the columns that are not available in the current data source of the grid and add those ones that are missing:
DataTable dt1 =
new
DataTable();
DataTable dt2 =
new
DataTable();
public
Form1()
{
InitializeComponent();
dt1.Columns.Add(
"A"
,
typeof
(
string
));
dt1.Columns.Add(
"B"
,
typeof
(
string
));
dt1.Columns.Add(
"C"
,
typeof
(
string
));
dt1.Columns.Add(
"D"
,
typeof
(
string
));
dt2.Columns.Add(
"A"
,
typeof
(
string
));
dt2.Columns.Add(
"B"
,
typeof
(
string
));
dt2.Columns.Add(
"C"
,
typeof
(
string
));
dt2.Columns.Add(
"E"
,
typeof
(
string
));
for
(
int
i = 0; i < 10; i++)
{
dt1.Rows.Add(
"A"
+ i,
"B"
+ i,
"C"
+ i,
"D"
+ i);
dt2.Rows.Add(
"A"
+ i,
"B"
+ i,
"C"
+ i,
"E"
+ i);
}
this
.radGridView1.DataSource = dt1;
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
private
void
radButtonSave_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
radButtonLoad_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);
//remove reduntant columns
DataTable dt =
this
.radGridView1.DataSource
as
DataTable;
for
(
int
i = 0; i <
this
.radGridView1.Columns.Count; i++)
{
GridViewDataColumn col =
this
.radGridView1.Columns[i];
if
(!dt.Columns.Contains(col.Name))
{
this
.radGridView1.Columns.Remove(col);
}
}
//add missing columns
foreach
(DataColumn col
in
dt.Columns)
{
if
(!
this
.radGridView1.Columns.Contains(col.ColumnName))
{
this
.radGridView1.Columns.Add(col.ColumnName);
this
.radGridView1.Columns[col.ColumnName].FieldName = col.ColumnName;
}
}
}
private
void
radButtonRebind_Click(
object
sender, EventArgs e)
{
this
.radGridView1.DataSource = dt2;
}
Note that this sample code snippet is purposed to demonstrate the approach and it may not cover all possible cases. Feel free to modify it on a way, which suits your requirement best.
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.