This step-by-step article illustrates how to implement and how to use a
DataSetHelper class that includes sample code to create a
DataTable object that contains the unique values of a column of another
DataTable object.
To do this, you use the SelectDistinct public method. You can also use a private helper method that compares fields that may contain NULL values (DBNull.Value).
The DataSetHelper class includes a DataSet member variable. Optionally, you can assign an existing DataSet object to the DataSet member variable. If the member variable points to a valid DataSet, any DataTable objects that the SelectDistinct method creates are added to the DataSet. In either case, the method call returns a reference to the DataTable object.
For additional information about DataSet objects, click the article number below to view the article in the Microsoft Knowledge Base:
To do this, you use the SelectDistinct public method. You can also use a private helper method that compares fields that may contain NULL values (DBNull.Value).
The DataSetHelper class includes a DataSet member variable. Optionally, you can assign an existing DataSet object to the DataSet member variable. If the member variable points to a valid DataSet, any DataTable objects that the SelectDistinct method creates are added to the DataSet. In either case, the method call returns a reference to the DataTable object.
For additional information about DataSet objects, click the article number below to view the article in the Microsoft Knowledge Base:
313485
(http://support.microsoft.com/kb/313485/EN-US/ ) INFO: Roadmap for ADO.NET DataSet, DataView, and DataViewManager
Requirements
- The fieldname and alias parts of the field list must comply with DataColumn naming conventions. The parser also restricts the names, in that the name must not contain a period (.), a comma (,), or a space ( ).
- If you click the button more than one time, the same table is added two times to the DataSet, which results in an exception. To workaround this problem, you can add code to the test application to check whether a DataTable of the same name already exists. Alternatively, you can create the DataSetHelper class without a reference to a DataSet, and then bind the DataGrid.DataSource property directly to the dt variable instead of by using the SetDataBinding method call.
- If the source table uses custom data types (that is, a class), you must add code to the SelectDistinct method to perform a deep copy of the data. Otherwise, only a reference is copied.