Results 1 to 2 of 2

Thread: [RESOLVED] Convert Dictionary to DataTable

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Resolved [RESOLVED] Convert Dictionary to DataTable

    Hello Everybody,

    How can I convert following categories object into DataTable, or alternatively how can I use the same to loop through individual values?

    Code:
    Dictionary<Guid, UHCategory> categories = new Dictionary<Guid, UHCategory>();
    categories = ClassProvider.GetCategoryCollection();
    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert Dictionary to DataTable

    First up, don't create objects that you never use. Why create a new Dictionary, only to throw it away the very next line with one you get from somewhere else? If GetCategoryCollection is going to give you a Dictionary object, why are you creating one on the previous line that you never use?

    As for the question, you would have to build the DataTable schema yourself, then loop through the items in the Dictionary and add a DataRow for each one. I don't know what a UHCategory is but, as an example:
    csharp Code:
    1. DataTable table = new DataTable();
    2.  
    3. table.Columns.Add("CategoryID", typeof(Guid));
    4. table.Columns.Add("CategoryName", typeof(string));
    5.  
    6. foreach (KeyValuePair<Guid, UHCategory> category in categories)
    7. {
    8.     table.Rows.Add(category.Key, category.Value.Name);
    9. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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