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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

79022 Jan 16,2009 04:02 PM UTC Jun 11,2010 10:39 AM UTC WinForms 3
loading
Tags: GridControl
Administrator [Syncfusion]
Asked On January 16, 2009 04:02 PM UTC

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


Nisha Arockiya A [Syncfusion]
Replied On 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.


Santosh Kandhukuri
Replied On June 10, 2010 04:37 PM UTC


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

Nirmal Raja [Syncfusion]
Replied On 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;