Creating a table in C#

Started by
3 comments, last by Winegums 16 years, 10 months ago
I have a c# windows form that I want to contain a table of items. this list of items can be added to or have items removed from it at any time, and contains (and displays) several items from the class (name, ID number, etc). Currently the data for this list is stored in an enumerated list ( based on: http://msdn2.microsoft.com/en-US/library/ms173233(VS.80).aspx). I want to display this in a table, and decided to use a ListBox to accomplish this (if theres a better way of displaying the information i'm all ears). However, ListBox throws an exception if you try to make it look at the list, and it seems it only likes DataSet items. I then made the data set, but I can't figure out how to add items to it (even just explicitly as strings). Anyone able to help with any of these issues?
Advertisement
Winegums,

First, I'd recommend using a ListView, rather than a ListBox. ListViews provide more flexibility in appearance and behavior than a ListBox, and you can set the ListView view to "Details" in order to have columns for each of the properties of the object you want displayed.

For more info, look HERE.

Secondly, your problem with binding to the list is that you're trying to bind to an IEnumerable generic. The Binding object requires the DataSource be either a DataSet, DataTable, DataView, DataViewManager, or object that implement the IList interface if you're trying to bind a set. Likely, you just need to store your data in something that implements IList. My personal preference is ArrayList if you dont need something that's strongly typed. If you do, you can of course create your own list based on the List<> generic.

Here's some more information on the binding.

Here's the info for ArrayList

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Also, be aware that if your class is a table entry in the database sense of the word, you can use xsd's to design your table layout - and use .Net tools to go through your xsd and create classes for you.

http://msdn2.microsoft.com/en-us/library/x6c1kb0s(vs.71).aspx
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
thanks for the replies, i certainly feel i'm on the right track now, but i'm sort of swamped in the whole data binding thing.

the examples i've looked at on MSDN have been a bit confusing. currently i have an ArrayList called arrayList. adding stuff to that seems to work fine which is cool.

but binding it to the ListView is the bit i'm not sure about. I figured the next step is to link the ListView to the ArrayList, but I'm not sure how use the Add() function. I don't understand what the parameters mean, and the examples on MSDN seem to work with data sets, which look like they're different from ArrayLists?
ok i've managed to figure out how to bind things, but what part of ListView am i using with DataBindings.Add() in order to link the data source to the ListView?

This topic is closed to new replies.

Advertisement