We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How can i persist the columns order of GridDataBoundGrid?? Please help

Hi,

I'm trying to figure out a way to persist the column ordering of GridDataBoundGrid.

I enable my users to change the order of the columns as well as hide / show columns, however, but whenI was trying to do it manually (i.e serialize it into a file),i couldn't even figure out how to get the current column position (as the order organize it) rather than the pre-defined columns.

Please let me know if you can help me with it and don't refer me to any Essential studio example; i tried them all and non of them have the same thing.

Thanks


3 Replies

NA Nisha Arockiya A Syncfusion Team January 19, 2009 10:05 AM UTC

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.



SK Santosh Kandhukuri June 10, 2010 04:37 PM UTC


In GridBoundColumn c = col[s]; where did you define col[] ?


NR Nirmal Raja Syncfusion Team June 11, 2010 10:39 AM UTC

Hi Santhosh,

Please refer the below modified code snippet that includes the declaration of the col[] collection:
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());
}
}


Let me know if you have any queries.

Regards,
Nirmal

Loader.
Live Chat Icon For mobile
Up arrow icon