Results 1 to 5 of 5

Thread: populating a combobox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Barcelona
    Posts
    26

    Question populating a combobox

    I must to fill a combobox. My data source is an xml file. Wich is the better system to populate the combobox? I can do it with the traditional AddItem method, but I don't know if there is a better manner to do it. Thank you.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I am not very good with XML yet, but you could put that xml into a dataset, then databind the dataset to the combobox. You may be able to databind to the xml file, but I wouldn't know how.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Barcelona
    Posts
    26

    Arrow

    it's a possibility. I will prove this implementation. Thank's

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    for example you can do something like this :
    Load all tables in an XML File to dataset , loop through it , add table names to combobox .
    Code:
    System.Data.DataSet ds=new DataSet();
    			ds.ReadXml("data.xml");
    			
    			for(int i = 0; i < ds.Tables.Count - 1; i++)
    			{
    				this.comboBox1.Items.Add(ds.Tables[i].TableName); 
    
    			}

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You can use databinding

    Code:
    DataSet ds = new DataSet();
    ds.ReadXml("books.xml");
    
    comboBox1.DataSource = ds.Tables["book"];
    comboBox1.DisplayMember = "title";
    comboBox1.ValueMember = "id";
    Here is the XML file
    PHP Code:
    <bookcollection>
        <
    book>
            <
    title>Vb.NET for Dummies</title>
            <
    id>12236</id>
        </
    book>
        <
    book>
            <
    title>C# For Dummies</title>
            
    <id>32163</id>
        </
    book>
        <
    book>
            <
    title>.NET Framework</title>
            <
    id>52632</id>
        </
    book>
    </
    bookcollection

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