CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    177

    Convert Generic List To Dataset

    Can anyone teach me how to convert the listobj to dataset?

    Code:
      ROLES_FORMSEN argEn = new ROLES_FORMSEN();
            ROLES_FORMS dalObj = new ROLES_FORMS();
    
            List<ROLES_FORMSEN> listObj = dalObj.GetList(argEn);
    
            GridView1.DataSource = listObj;
           
    
            GridView1.DataBind();

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Convert Generic List To Dataset

    Quote Originally Posted by daniel50096230 View Post
    Can anyone teach me how to convert the listobj to dataset?...
    A List<something> never can be a Dataset !
    Dataset is a collection of DataTables so you cannot convert it that way.

    If you want to bind data to a Grid then simple fill a Dataset directly from the database using DataAdapter. Or if needed create a DataTable and fill it by use of code with all the data of your List.
    This DataTable you can bind to your Grid
    Last edited by JonnyPoet; May 21st, 2010 at 03:52 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Jan 2009
    Posts
    177

    Re: Convert Generic List To Dataset

    Do you have any sample on fill the data from list into datatable??Because I am not very good in datatable..

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Convert Generic List To Dataset

    Quote Originally Posted by daniel50096230 View Post
    Do you have any sample on fill the data from list into datatable??Because I am not very good in datatable..
    You can use a foreach loop to go through the list
    At first you need to create a DataTable which contains each of the columns your gridview should contain. This needs to be exactly defined just like a table in a database name of Column, type of data... then in your loop you simple add DataRows to your DataTable by filling each Row with the needed Values.
    There are lots of DataTable examples here in the forum IMHO. Simple search for DataTable
    Here is one for creation of a DataTable
    http://dotnetperls.com/datatable-use
    Last edited by JonnyPoet; May 21st, 2010 at 03:55 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    May 2010
    Posts
    3

    Arrow Re: Convert Generic List To Dataset

    Hi,

    There is an way to convert a generic collection to datatable. With this you can populate a datatable with columns (properties of the collection) and rows (values of the properties).

    Here is the code to do the same.

    public static DataTable ConvertTo<T>(IList<T> genericList)
    {
    //create DataTable Structure
    DataTable dataTable = CreateTable<T>();
    Type entType = typeof(T);
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
    //get the list item and add into the list
    foreach (T item in genericList)
    {
    DataRow row = dataTable.NewRow();
    foreach (PropertyDescriptor prop in properties)
    {
    row[prop.Name] = prop.GetValue(item);
    }
    dataTable.Rows.Add(row);
    }
    return dataTable;
    }

    public static DataTable CreateTable<T>()
    {
    //T –> ClassName
    Type entType = typeof(T);
    //set the datatable name as class name
    DataTable dataTable = new DataTable(entType.Name);
    //get the property list
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
    foreach (PropertyDescriptor prop in properties)
    {
    //add property as column
    dataTable.Columns.Add(prop.Name, prop.PropertyType);
    }
    return dataTable;
    }

    Now with this code you can convert any generic list to a datatable.

    Regards,
    Jayashree K R.

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Convert Generic List To Dataset

    Quote Originally Posted by contactshree View Post
    Hi,
    There is an way to convert a generic collection to datatable. .....
    Great. seems to be what I have tried to describe. But please can you edit this post and use codetags so the code stays correctly formatted and easy to be read ?
    Last edited by JonnyPoet; May 21st, 2010 at 11:45 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    May 2010
    Posts
    3

    Re: Convert Generic List To Dataset

    Thanks for the reply. This is my first post, I missed it.

    Code:
             public static DataTable ConvertTo<T>(IList<T> genericList)
            {
                //create DataTable Structure
                DataTable dataTable = CreateTable<T>();
                Type entType = typeof(T);
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
                //get the list item and add into the list
                foreach (T item in genericList)
                {
                    DataRow row = dataTable.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                    {
                        row[prop.Name] = prop.GetValue(item);
                    }
                    dataTable.Rows.Add(row);
                }
                return dataTable;
            }
    
            public static DataTable CreateTable<T>()
            {
                //T –> ClassName
                Type entType = typeof(T);
                //set the datatable name as class name
                DataTable dataTable = new DataTable(entType.Name);
                //get the property list
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
                foreach (PropertyDescriptor prop in properties)
                {
                    //add property as column
                    dataTable.Columns.Add(prop.Name, prop.PropertyType);
                }
                return dataTable;
            }

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Convert Generic List To Dataset

    Thx for adding Codetags. Great help for the poster, you will get my points
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Dec 2007
    Posts
    234

    Re: Convert Generic List To Dataset

    Oooooh.... if this works hlaf as well as I think it will, I might be able to use this.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured