Click here to Skip to main content
Licence 
First Posted 2 May 2005
Views 68,636
Downloads 546
Bookmarked 28 times

How to Convert a Collection Base to a DataSet

By | 15 Jun 2007 | Article
An article on converting a Collection Base to a DataSet.
 
Part of The SQL Zone sponsored by
See Also

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.

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:

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:

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?

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

About the Author

Okta Endy

Web Developer

Indonesia Indonesia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUse it with a bindingsource? [modified] Pinmemberalan933:12 26 Jan '10  
GeneralRe: Use it with a bindingsource? Pinmemberalan935:41 27 Jan '10  
QuestionDoesn't work for DataGridView? Pinmemberalan938:29 6 Jan '10  
AnswerRe: Doesn't work for DataGridView? PinmemberOkta Endy17:22 6 Jan '10  
GeneralRe: Doesn't work for DataGridView? Pinmemberalan939:11 8 Jan '10  
Questionexception - System.NullReferenceException was unhandled Pinmemberpmontu20:46 21 Oct '09  
AnswerRe: exception - System.NullReferenceException was unhandled PinmemberOkta Endy16:19 6 Jan '10  
Generalconvert to vb Pinmemberslayer_stb15:24 13 Jun '07  
GeneralRe: convert to vb PinmemberOkta Endy18:08 13 Jun '07  
NewsRe: convert to vb PinmemberOkta Endy16:13 17 Jun '07  
GeneralRe: How should you cast data as a datatype instead of object type PinmemberL8Again12:59 15 Aug '06  
GeneralRe: How should you cast data as a datatype instead of object type PinmemberOkta Endy15:43 15 Aug '06  
QuestionHow should you cast data as a datatype instead of object type PinmemberL8Again4:27 27 Apr '06  
AnswerRe: How should you cast data as a datatype instead of object type PinmemberOkta Endy0:28 28 Apr '06  
QuestionWhat is CollectionBaseCustom PinmemberSteven Bristol6:56 9 May '05  
AnswerRe: What is CollectionBaseCustom PinmemberOkta Endy18:07 9 May '05  
QuestionRe: What is CollectionBaseCustom PinmemberMel Shellam4:11 15 Apr '08  
AnswerRe: What is CollectionBaseCustom PinmemberOkta Endy16:18 17 Apr '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120515.1 | Last Updated 15 Jun 2007
Article Copyright 2005 by Okta Endy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid