#1
  1. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    110

    C# Load from ArrayList into datagrid


    I have an ArrayList() containing a load of objects. The objects are addressbook entries basically consisting of a 'string name' and 'string email'. When my Windows Forms application loads, it reads these entries from the arraylist and loads the names into a listBox. That bit is very simple.

    The problem I'm having is with the next stage in the application, where the user double clicks on an entry in the listbox and the details for that 'name' are loaded into a datagrid alongside. What I need to do here is just copy the selected entry into another ArrayList() which represents the data displayed in the grid.

    This is where I get stuck. Firstly, I can't work out how to fix the column headers onto the datagrid - are these determined from field names in the datasource, or should I be hardcoding these in using the designer in VS? Secondly, should I be using a datagrid at all? Perhaps there is a different component which I have overlooked? and Thirdly, how can I get the datagrid ether to respond to updates to an ArrayList of data, or to programatically add and remove and update rows?

    I realise this is a long winded question, but my hours of google and msdn haven't come up with anything that really answers my basic question. I have 3 books on order from amazon. They arrive on Friday

    thanks

    christo
    This is me: http://chris.uk.com
  2. #2
  3. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    110
    okay I have got something working here using
    Code:
    dataGrid1.DataSource = addressList;
    dataGrid1.Refresh();
    However, I'm not sure if the datagrid is really what I should be using here. The component seems to be rather unwieldly for my smiple display task. It obediently displays all the fields in my ArrayList, where I only want to see 2 of them (ie it shows the id, name, email and status, but I only want to show name and status columns).. I'll explore some more and come back with a more refined question later.

    ta
    christo
    This is me: http://chris.uk.com
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    100
    Rep Power
    10
    How is the Addresslist object looking like ?
  6. #4
  7. Introspective
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    3,317
    Rep Power
    110
    The addressList object is an ArrayList. It contains address objects which look like this:

    Code:
    	public class Address{
    		private string name, destination, email, id;
    
    		public Address(){
    		}
    
    		public string Name { // this will be displayed in the column Header in the DataGrid 
    			get {
    				return name; // this will be displayed in the data field of the DataGrid 
    			}
    			set {
    				name =  value;
    			}
    		}
    		public string Id { // this will be displayed in the column Header in the DataGrid 
    			get {
    				return id; // this will be displayed in the data field of the DataGrid 
    			}
    			set {
    				id =  value;
    			}
    		}
    		public string Email { // this will be displayed in the column Header in the DataGrid 
    			get {
    				return email; // this will be displayed in the data field of the DataGrid 
    			}
    			set {
    				email =  value;
    			}
    		}
    		public string Number { // this will be displayed in the column Header in the DataGrid 
    			get {
    				return destination; // this will be displayed in the data field of the DataGrid 
    			}
    			set {
    				destination =  value;
    			}
    		}
    christo
    This is me: http://chris.uk.com
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    226
    Rep Power
    12
    you need to set TableStyle for the databrid. Make sure that the table style's mapping name is "ArrayList". Then you need to create 2 GridColumnStyles. "HeaderText" refers to column header and "MappingName" refers the the public property in the Address class.

IMN logo majestic logo threadwatch logo seochat tools logo