Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to serialize a previously untyped DataSet that has been modified by another Component in Design time?
 

How to serialize a previously untyped DataSet that has been modified by another Component in Design time?

Hi,

I placed a DataSet and a self-made Component (let's call it "MyComponent") in the component area of a form. MyComponent has a DataSet as Property and I chose the DataSet from the component area for it.

In Design Time of the Form the MyComponent adds a DataTable to the (previously untyped) DataSet.

After this has been done, the added DataTable can be seen in the Tables collection of the DataSet component.

However, the added DataTable doesn't find it's way to the .designer file and thus after closing the form and reopening, the DataSet's Tables collection is empty again.

When I add a DataTable via the DataSet property grid, the added DataTable remains (has been written to the .designer file).

Does anyone know how I could achieve the addition of the DataTable through my other component being stored (serialized) in the .designer file?

P.S.: When I let MyComponent add a DataTable and after that I add another DataTable by using the DataSet's property grid, only the latter gets serialized.

Any help would be much appreciated
MK2k  Wednesday, June 10, 2009 11:47 AM

Hi Mahender,

From my experience, you need to add the DesignerSerializationVisibility attibute to the DataSet property. The code snippet below shows how to do that.

public class MyComponent : Component

{

private DataSet _ds = new DataSet();

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

public DataSet MyDataSet

{

get { return _ds; }

set { _ds = value; }

}

}

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, June 11, 2009 10:17 AM
Hi Aland,

adding DesignerSerializationVisibility.Content (instead of .Visible) unfortunately doesn't work. If I do that, the added DataTable doesn't get serialized.

But also the assignment of the DataSet in the Components area to the Form to MyComponent doesn't get serialized too.
MK2k  Thursday, June 11, 2009 12:32 PM

Hi Mahender,


Could you please provide me with a code snippet? It works fine in my project. By the way,could youtell me what version of Visual Studio and dot net framework you are using? This can help me testin a more accurate environment. Thanks.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

Aland Li  Thursday, June 11, 2009 1:16 PM
Hi Aland,

as the project has grown quite large, I will try to get a small snippet together in the next days (downloadable as a zipped VB project then). I hope you get alerted on new posts in this thread and thank you for your time :)

I'm using VS2005 .Net Framework 2.0 at the moment
MK2k  Thursday, June 11, 2009 1:38 PM

Hi Mahender,

You don't need to provide me with the whole project, but only the key code snippet. Please isolate the key code in a new project, so that I can test in the same environment as yours. Thanks.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, June 12, 2009 3:35 AM
Ok, here is the example project: http://mk2k.net/temp/ComponentTest.zip
VS2005, VB, .Net 2.0

After opening the project, compile it completely.

When you open Form1 in the Designer you see the DataSet DS being typed by hand with a table "Table1" (which got serialized properly).
Then you have MyCompoenent, already having DS as the DataSet member. And another member called doType, which is a String and preset to "type!".

If you enter some value for the doType member, MyComponent will reset it to "type!" and will add a new Table called "Autotable" to the DataSet. This new table will not be serialized by the Designer.

I hope you or someone else could help me finding a way to let the Designer serialize "Autotable" for DS in the same way as it's done with "Table1".
MK2k  Sunday, June 14, 2009 1:35 PM

Hi MK2k,

Thank you for sending me the source code. I am working on it and will reply to you soon.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, June 15, 2009 10:29 AM

Hi MK2k,

I have analyzed your source code and find it does not work as you said. Based on my understanding, you wanted to generate codes to add a table to the dataset in you custom. You tried to do that by setting the value of a property in the properties window, but it didn’t work. I suggest that you can edit the DS property directly in the properties window and add a DataTable on the Tables Collection Editor dialog. If I misunderstood you, please feel free to tell me.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, June 16, 2009 1:29 AM
Hi Aland,

setting the table does work (just try to alter the text for the doType member and press Enter), the DataSet will then have a new table called "Autotable". My problem is, how could this new table be serialized in the .designer file?
MK2k  Tuesday, June 16, 2009 11:07 AM

Hi Mahender,

I think we need to answer the questions below:

1. What can the designer do for a component?

2. If the codes are generated as what you wanted, what will it result in?

My answers:

1. A component is an object, which has members and methods. The methods indicates what the component can do, however, the members stores its data which can be visited by some properties. The designer only provides a visual way to modify the data of a component via its properties and generates the relevant codes , nothing else. The values of the properties can be set in the properties window or by smart tags. When you set the value of the doType property, the designer only generates the codes to set the doType property, not including what has done in the set function. The designer cannot know the details of the set function of the doType property, because it can only see the interface of the component, which includes the properties that can be visited by other objects(the modifier is public or internal). For these reasons, the designer cannot generate codes as you wanted.

2. If the codes are generated, it will generate an incorrect result: the codes are actually executed twice. For example, the codes will be like this:

Me.MyComponent.doType = "type!"

Me.MyComponent.DS.Tables.Add("AutoTable")

In the first line, the table is added in the set function of the doType property. In the second line, the table is added again, which was incorrect.


Based on my understanding, you didn’t want the DataSet to be edited directly in the designer. You may only wanted the tables to be edited in the designer. You can hide the DataSet property and add another property to allow the designer to edit the tables in the designer as follows:

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _

Public Property DS() As DataSet

Get

Return Me._DS

End Get

Set(ByVal value As DataSet)

Me._DS = value

End Set

End Property

Private _tables As List(Of DataTable) = New List(Of DataTable)

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _

Public ReadOnly Property Tables() As List(Of DataTable)

Get

Return _tables

End Get

End Property

You can add the tables into the DataSet after the InitializeComponent method was executed such as in the Load event handler of the form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

For Each dt As DataTable In Me.Cls_MyComponent1.Tables

Me.Cls_MyComponent1.DS.Tables.Add(dt)

Next

End Sub

Let me know if this helps.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.

Aland Li  Wednesday, June 17, 2009 7:43 AM
Hi Aland,

thanks for the long reply, I will deeply look into what you suggest here.
I want the DataSet to be serialized in the designer because when I want to use a BindingSource later, the Designer throws an error when the table isn't available in the DataSet, so I think that using a function that handles the Load event isn't usable here. But as I said, I will have a look and report later.

best regards

-- MK2k
MK2k  Wednesday, June 17, 2009 3:20 PM

Hi Mahender,

Based on my understanding, the best way to do the complicated logic is writing codes manually, not in designer.

If you have to do it in designer, you can add a BindingSource component in the designer, make all its members initialized, and then use it in other components.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, June 18, 2009 7:19 AM

You can use google to search for other answers

Custom Search

More Threads

• Controls and Generics?
• DataGridView Rows reappearing
• Creating a draggable control?
• WinForms designer throws Exception when adding fillToolStrip component from DataSet without TableAdapters
• Password in PropertyGrid
• VB 2008 Create Buttons at Runtime with Event Handlers
• Error in designer when adding label to FlowLayout Panel
• help: Creating Custom ToolStripButton, implementing IButtonControl
• Difference between arrays and objects when referencing controls
• Inherited Click handler