0

I have a small dataset which has most of its schema defined at design time, but I add a few columns to one of its tables at runtime. My problem is how do I reset those changes?

The dataset is not linked to any datasource. It just has its own table which I "manually" populate. The static columns have been added to the datatable at design time. However, I add a few columns as runtime based on a list of categories. Like this:

For Each aCategory In theCategories
    theDataTable.Columns.Add(aCategory.CategoryName, System.Type.GetType("System.Boolean"))
Next

The dataset is used in a small dialog that pops up within my application. I add the dynamic columns when the dialog is opened by using the Load event. Everything works great the first time. But if you close the dialog and reopen it, there are problems. My startup routine tries adding the dynamic columns again, but an error is thrown saying the column already exists. I have tried a lot of things and did some reading but I can't figure out a way to reset the dataset when the dialog is closed. The only solution is to close the whole application and reopen it. I have tried the following in the Leave event of the dialog form:

theDataSet.Reset()
theDataSet.Dispose()
theDataSet.RejectChanges()

None of these seem to make a difference and the error continues. I have also tried applying the same methods to the table in addition to the dataset. Such as:

theDataSet.Tables(0).Reset()
etc.

Any ideas on how to completely obliterate the dataset when the form closes? I gave my examples in VB, but I can digest answers in either VB or C#.

1
  • Don't waste your brain power solving this issue. I have answered my own question with a very definite solution. However, due to my low reputation I cannot post an answer to my own question for another 6 hours.
    – ThinkerIV
    May 16, 2012 at 2:06

3 Answers 3

0

Maybe I am not understanding your implementation fully, but "design time" datasets are regular classes and I assume "theDataSet" in the code you show and the methods you call is an instance of said classes.

If you want to completely obliterate your dataset (instance) with any change you made to it you can simply create another instance.

theDataset = new DatasetClassCreatedByTheDesigner();
1
  • I am aware of that option and wondered if I could use it. However, I created the dataset using drag and drop in the designer. I am linking a datagridview to the dataset in the designer, so I am just using the object created by the designer directly. without creating a new object from it. Hence I can't use the new method. Is this a bad practice?
    – ThinkerIV
    May 16, 2012 at 1:01
0

There are several possible ways of solving your problem. I.e:

  • if your dynamic columns has been created, remove them
  • chek if your dynamic columns has been created before adding them. If they have been created, reuse them, instead of adding new columns

For both options you need to add extra information to the DataSet. You can do that using the DataSet.ExtendedProperties Property.

By the way, your code is using a DataTable, not a DataSet. DataSets doesn't have columns. They instead have DataTables, and those are which have the columns. They also have a ExtendedProperties property, so that you can use this solution.

In the firs solution, you can add a extended property with the original number of columns. So that you can easily make a loop that removes the last column until the number of columns is equal to the original (your code adds the new columns at the end of the columns collection). You can uses RemoveAt(dataTable.Columns.Count -1).

In the second solution, you can use a simple extended property holding a boolean that informs you if the dynamic columns has been added or not.

You can implement this solutions in a method that receives the DataTable as a parameter, so that you easily reuse your code. You can even create an extension method if you prefer.

Implementation of the second solution:

public static void RemoveExtraColumns(DataTable dt)
{
  // get original column count from extended property
  while(dt.Columns.Count > originalColumnCount)
    dt.Columns.RemoveAt(dt.Columns.Count -1);
}

If you want to use it as an extension method, implement it in an static class, and change its signature to this:

public static void RemoveExtraColumns(this DataTable dt)
1
  • Thanks for pointing out my error with datasets vs. datatables. I am aware of the difference and it was just an error on my part when creating the post. I have edited the post. I thought of using the methods you are presenting, and have actually used them before in other implementations. However, I thought there has to be a more streamlined way to reset a dataset back to its design time default.
    – ThinkerIV
    May 16, 2012 at 1:08
0

I have answered my own question. It turns out that my problem was not really anything to do with datasets or datatables but rather dialog boxes.

I was calling the form in which the dataset was used as follows:

ActionsSettingsForm.ShowDialog()

After reading this article: http://msdn.microsoft.com/en-us/library/aw58wzka.aspx I realized that the dispose method is only automatically called if you use .show not .showdialog. So i changed the code that called the form to:

ActionsSettingsForm.ShowDialog()
ActionsSettingsForm.Dispose()

And it solved the problem. Now all the objects associated with the form are properly disposed when the form closes. Which means that the dataset is reset to its design time default, or rather a new one is created when the form is opened again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.