Click here to Skip to main content
15,898,371 members
Articles / Desktop Programming / Windows Forms
Article

How to Convert a Collection Base to a DataSet

Rate me:
Please Sign up or sign in to vote.
2.93/5 (15 votes)
15 Jun 2007 108.8K   807   29   19
An article on converting a Collection Base to a DataSet.

Screenshot - CollectionBaseToDataSet.GIF

Introduction

This article shows us how to convert a Collection Base to a DataSet. It uses the System.Reflection and System.Collection namespaces.

Background

Sometimes we need to convert a Collection Base to a DataSet and we need to use the DataSet's functionality such as filtering, sorting and others which are not available in a Collection Base. That's why I created this code. I hope it will help other developers.

Using the code

First, you need to create a class that inherits from CollectionBaseCustom. CollectionBaseCustom is inherited from System.Collection.CollectionBase. You will find it in the source code available above.

C#
using System;

[Serializable]
public class Book
{
    public Book()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private string _BookId;
    public string BookId
    {
        get
        {
            return _BookId;
        }
        set
        {
            _BookId = value;
        }
    }

    private string _BookName;
    public string BookName
    {
        get
        {
            return _BookName;
        }
        set
        {
            _BookName = value;
        }
    }

    private string _BookAuthor;
    public string BookAuthor
    {
        get
        {
            return _BookAuthor;
        }
        set
        {
            _BookAuthor = value;
        }
    }
}

public class BookCollection:CollectionBaseCustom
{
    public void Add(Book oBook)
    {
        this.List.Add(oBook);
    }
    public Book this[Int32 Index]
    {
        get
        {
            return (Book)this.List[Index];
        }
        set
        {
            if (!(value.GetType().Equals(typeof(Book))))
            {
                throw new Exception("Type can't be converted");
            }
            this.List[Index] = value;
        }
    }
}

Now you fill the collection with data:

C#
private BookCollection FillData()
{
    BookCollection oBookCollection = new BookCollection();

    for(int i = 0; i < 100; i++)
    {
        Book oBook = new Book();
        oBook.BookId = i.ToString();
        oBook.BookName = "Name" + i.ToString();
        oBook.BookAuthor = "Author" + i.ToString();

        oBookCollection.Add(oBook);
    }
    return oBookCollection;
}

When you need to convert a Collection Base to a DataSet and bind it to a DataGrid, you just write this code:

C#
this.grdForm.DataSource = FillData().ToDataSet().Tables[0].DefaultView;

After that, you can use all of the DataSet's functionality, such as filtering, just like this! Easy, huh?

C#
if (this.grdForm.DataSource != null)
{
    DataView oDataView = (DataView)this.grdForm.DataSource;
    oDataView.RowFilter = "BookId > 22 AND BookId < 88 ";

    this.grdForm.DataSource = oDataView.Table.DefaultView;
}

History

  • 3 May, 2005 -- Original version posted
  • 10 May, 2007 -- Updated
  • 27 May, 2005 -- Article moved
  • 15 June, 2007 -- Demo project download updated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Indonesia Indonesia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalre:Great Pin
Member 349360629-Jun-12 9:02
Member 349360629-Jun-12 9:02 
GeneralUse it with a bindingsource? [modified] Pin
alan9326-Jan-10 3:12
alan9326-Jan-10 3:12 
I added a bindingsource to the project but can't seem to get the obookcolletion to update when the grid cell cell changes.

What would you bind to? The book class right?

Here's what I did but it doesn't seem to update the collection:
//this.grdForm.DataSource = FillData().ToDataSet().Tables[0].DefaultView;
this.bookBindingSource.DataSource = FillData().ToDataSet().Tables[0].DefaultView;
this.grdForm.DataSource = this.bookBindingSource;


modified on Tuesday, January 26, 2010 9:45 AM

GeneralRe: Use it with a bindingsource? Pin
alan9327-Jan-10 5:41
alan9327-Jan-10 5:41 
QuestionDoesn't work for DataGridView? Pin
alan936-Jan-10 8:29
alan936-Jan-10 8:29 
AnswerRe: Doesn't work for DataGridView? Pin
Okta Endy6-Jan-10 17:22
Okta Endy6-Jan-10 17:22 
GeneralRe: Doesn't work for DataGridView? Pin
alan938-Jan-10 9:11
alan938-Jan-10 9:11 
Questionexception - System.NullReferenceException was unhandled Pin
pmontu21-Oct-09 20:46
pmontu21-Oct-09 20:46 
AnswerRe: exception - System.NullReferenceException was unhandled Pin
Okta Endy6-Jan-10 16:19
Okta Endy6-Jan-10 16:19 
Generalconvert to vb Pin
slayer_stb13-Jun-07 15:24
slayer_stb13-Jun-07 15:24 
GeneralRe: convert to vb Pin
Okta Endy13-Jun-07 18:08
Okta Endy13-Jun-07 18:08 
NewsRe: convert to vb Pin
Okta Endy17-Jun-07 16:13
Okta Endy17-Jun-07 16:13 
GeneralRe: How should you cast data as a datatype instead of object type Pin
L8Again15-Aug-06 12:59
L8Again15-Aug-06 12:59 
GeneralRe: How should you cast data as a datatype instead of object type Pin
Okta Endy15-Aug-06 15:43
Okta Endy15-Aug-06 15:43 
QuestionHow should you cast data as a datatype instead of object type Pin
L8Again27-Apr-06 4:27
L8Again27-Apr-06 4:27 
AnswerRe: How should you cast data as a datatype instead of object type Pin
Okta Endy28-Apr-06 0:28
Okta Endy28-Apr-06 0:28 
QuestionWhat is CollectionBaseCustom Pin
Steven A Bristol9-May-05 6:56
Steven A Bristol9-May-05 6:56 
AnswerRe: What is CollectionBaseCustom Pin
Okta Endy9-May-05 18:07
Okta Endy9-May-05 18:07 
QuestionRe: What is CollectionBaseCustom Pin
Mel Shellam15-Apr-08 4:11
Mel Shellam15-Apr-08 4:11 
AnswerRe: What is CollectionBaseCustom Pin
Okta Endy17-Apr-08 16:18
Okta Endy17-Apr-08 16:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.