Hi Ori,
Thanks for your interest in Syncfusion Products.
One way you can do this is to use the column MappingName. Given this string, you can use it as an indexer into either DataTable.Columns or a DataRow to pick out the desired column from your datasource. You can also use it as an indexer into GridDataBoundGrid.GridBoundColumns or GriddataBoundGrid.Binder.InternalColumns if needed.Here is code that will retrieve this mapping name given a column index, nCol, numbered 1 to grid.Model.ColCount.
string colMappingName = this.gridDataBoundGrid1.Binder.InternalColumns[this.gridDataBoundGrid1.Binder.ColIndexToField(nCol)].MappingName;
As far as persisting the column order, we currently don’t offer any direct support for this, but it is on our to-do list.Probably, the best way to persist this is to save a string array that holds the column MappingNames in the order you want them. Below is some sample code you might try.
private void Form2_Load(object sender, System.EventArgs e)
{
if( File.Exists("MySizes.bin"))
{
try
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MySizes.bin", FileMode.Open, FileAccess.Read, FileShare.None); //handle the mappingnames GridBoundColumnsCollection col = this.gridDataBoundGrid1.Binder.InternalColumns;
string[] a = (string[]) formatter.Deserialize(stream);
this.gridDataBoundGrid1.GridBoundColumns.Clear();
foreach(string s in a)
{
GridBoundColumn c = col[s];
this.gridDataBoundGrid1.GridBoundColumns.Add(c);
}
this.gridDataBoundGrid1.Model.ColWidths.Dictionary =(Syncfusion.Windows.Forms.Grid.GridRowColSizeDictionary) formatter.Deserialize(stream); stream.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
//serialize out the row heights & columnwidths
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MySizes.bin", FileMode.Create, FileAccess.Write, FileShare.None); //handle the mappingnames int nCols = this.gridDataBoundGrid1.Binder.InternalColumns.Count;
string[] a = new string[nCols];
int i = 0;
foreach(GridBoundColumn c in this.gridDataBoundGrid1.Binder.InternalColumns) a[i++] = c.MappingName;
formatter.Serialize(stream, a);
formatter.Serialize(stream, this.gridDataBoundGrid1.Model.ColWidths.Dictionary); stream.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Please let me know if this serve your needs.
Regards,
Nisha.