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

resetting the DataSource in a GridDataBoundGrid

I am currently trying to reset the datasource in a GridDataBoundGrid to a new datasource. I am doing this as I am changing the structure of the dataset that the datasource points to at runtime (extra columns and rows added).

I have tried the code below but it does not work as I get an exception at the last line of code (also pasted beneath).

//dataModel is an object of custom type Models
DataSet = new DataSet();
serializer = new XmlSerializer(typeof(Models));
mStream = new MemoryStream();
serializer.Serialize(mStream,dataModels);
mStream.Position = 0;
ds.ReadXml(mStream);

gridDataBoundGrid1.DataMember = "";
gridDataBoundGrid1.DataSource = null;
gridDataBoundGrid1.Binder.ResetHierarchyLevels();
gridDataBoundGrid1.DataSource = ds;
gridDataBoundGrid1.DataMember = ds.Tables["Model"].TableName;

---------------------------------------------
Exception Received:
An unhandled exception of type ''System.FormatException'' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.
---------------------------------------------

I hope that someone can help as I''m getting quite frustrated.

6 Replies

AD Administrator Syncfusion Team September 25, 2006 06:30 AM UTC

Hi Tim,

Try this code to reset the datasource in a grid.

gridDataBoundGrid1.BeginUpdate();
gridDataBoundGrid1.Binder.SuspendBinding();

gridDataBoundGrid1.DataMember = "";
gridDataBoundGrid1.DataSource = null;

//Clear the Column and it''s type.
gridDataBoundGrid1.Binder.InternalColumns.Clear();

gridDataBoundGrid1.Binder.ResetHierarchyLevels();
gridDataBoundGrid1.DataSource = ds;
gridDataBoundGrid1.DataMember = ds.Tables["Model"].TableName;

gridDataBoundGrid1.EndUpdate();
gridDataBoundGrid1.Binder.ResumeBinding();

Let me know if this helps,

Best Regards,
Haneef


TD Timothy Duly September 25, 2006 07:37 AM UTC

Hi,

I tried the code that you posted and I still get the same error when trying to set the datamember.

If I switch the assignment ordering of DateMember and DataSource around, the exception is thrown on the last executable statement so it is not an error that is exclusive to the DataMember assignment itself.

I am using Syncfusion v3.3.0.0 if that helps.

Regards,

Tim


>Hi Tim,

Try this code to reset the datasource in a grid.

gridDataBoundGrid1.BeginUpdate();
gridDataBoundGrid1.Binder.SuspendBinding();

gridDataBoundGrid1.DataMember = "";
gridDataBoundGrid1.DataSource = null;

//Clear the Column and it''s type.
gridDataBoundGrid1.Binder.InternalColumns.Clear();

gridDataBoundGrid1.Binder.ResetHierarchyLevels();
gridDataBoundGrid1.DataSource = ds;
gridDataBoundGrid1.DataMember = ds.Tables["Model"].TableName;

gridDataBoundGrid1.EndUpdate();
gridDataBoundGrid1.Binder.ResumeBinding();

Let me know if this helps,

Best Regards,
Haneef


TD Timothy Duly September 25, 2006 08:41 AM UTC

I originally had ShowTreeLines set to True and after reading around noticed that this had caused a problem for others also. If I set ShowTreeLines to ''false'' then I no longer get the FormatException error but code that I ran for showing/expanding and collapsing columns no longer referenced the correct columns and the column displays were all out of kilter.

So currently the only way to get the grid to behave properly is still to destroy the old grid and dataset and create new instances of them every time I change the dataset.


AD Administrator Syncfusion Team September 25, 2006 10:50 AM UTC

Hi Tim,

Can you try the code below, to see if this helps in resetting the datasource for the grid.

this.gridDataBoundGrid1.BeginUpdate();
this.gridDataBoundGrid1.DataSource = null;
this.gridDataBoundGrid1.DataMember = "";
this.gridDataBoundGrid1.Binder.ResetHierarchyLevels();
this.gridDataBoundGrid1.DataSource = newDataSet.Tables[0];
this.gridDataBoundGrid1.Binder.InitializeColumns();
this.gridDataBoundGrid1.EndUpdate();

Thanks,
Rajagopal


LR Leema Rose November 27, 2006 01:13 PM UTC

While setting ShowTreeLines property to true when changing the dataset.

Exception occurs - how to handle it.




AD Administrator Syncfusion Team November 28, 2006 06:53 AM UTC

Hi Leema,

Thanks for the update.

One way around the problem is to turnoff the ShowTreeLines right after your grid.BeginUpdate call, and then turn it back on later right before your grid.EndUpdate.

this.gridDataBoundGrid1.Binder.SuspendBinding();
this.gridDataBoundGrid1.BeginUpdate();
gridDataBoundGrid1.ShowTreeLines = false;
....
this.gridDataBoundGrid1.Binder.ResumeBinding();
gridDataBoundGrid1.ShowTreeLines = true;
this.gridDataBoundGrid1.EndUpdate();

Best Regards,
Haneef

Loader.
Live Chat Icon For mobile
Up arrow icon