Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the questions

how to bind Parent And Child Combobox from one Xml file .Selecion of one Combobox second Combobox will be selected.

here is the xml file

XML
<?xml version="1.0" encoding="utf-8"?>
<Countries>
   <Country Id="1" Name="India">
     <State>
        <SId>1</SId>
        <SName>Maharashtra</SName>
     </State>
    <State>
       <SId>2</SId>
       <SName>Gujarat</SName>
    </State>
      <State>
         <SId>3</SId>
         <SName>Delhi</SName>
      </State>
     <State>
         <SId>4</SId>
         <SName>Uttar Pradesh</SName>
     </State>
       <State>
         <SId>5</SId>
         <SName>Kerala</SName>
       </State>
     <State>
       <SId>6</SId>
       <SName>Jammu and Kashmir</SName>
     </State>
  </Country>
<Country Id="2" Name="Pakistan">
     <State>
       <SId>1</SId>
       <SName>Lahore</SName>
     </State>
  <State>
     <SId>2</SId>
     <SName>Rawalpindi</SName>
  </State>
  <State>
    <SId>3</SId>
    <SName>Karachi</SName>
  </State>
     <State>
       <SId>4</SId>
       <SName>Islamabad</SName>
     </State>
  <State>
    <SId>5</SId>
    <SName>Multan</SName>
  </State>
  </Country>
<Country Id="3" Name="Sri Lanka">
     <State>
       <SId>1</SId>
       <SName>Colombo</SName>
     </State>
  <State>
    <SId>2</SId>
    <SName>Kandy</SName>
  </State>
  <State>
    <SId>3</SId>
    <SName>Galle</SName>
  </State>
     <State>
       <SId>4</SId>
       <SName>Anuradhapura</SName>
     </State>
  <State>
    <SId>5</SId>
    <SName>Ratnapura</SName>
  </State>
   </Country>
</Countries>
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jan-12 2:34am    
I wonder how one combo box can be a parent of another? Perhaps wrong wording?
--SA
lukeer 25-Jan-12 2:58am    
I guess that one combo shall hold countries. The second combo shall hold states in the country selected in first combo.

Therefore "Parent" is not meant the several-child-controls-in-a-parent-control-way but rather as a semantical connection.

Is that right, phoolchand yadav?
phoolchand yadav 25-Jan-12 4:04am    
ok u can tell accoding to your way but i required selection of one combobox second combobox will be selected.if this xml file is wrong you can tell according to your way
Amir Mahfoozi 25-Jan-12 8:52am    
Winform ? Web application ?

1 solution

Here is the solution by phoolchand

C#
namespace WinformswithXml
{
    public partial class Form4 : Form
    {
        string xmlfile = "D:\\phoolchand\\Country.xml";
        DataSet ds = new DataSet();

        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            
            DataView dv,dv1;
            

            ds.ReadXml(xmlfile);
            dv = new DataView(ds.Tables[0]);
            cboCountry .DataSource = dv;
            cboCountry.DisplayMember = "Name";
            cboCountry.ValueMember = "Country_Id";

            dv1 = new DataView(ds.Tables[1]);
            dv1.RowFilter = "Country_Id=" + cboCountry.SelectedValue.ToString();
            cbostate.DataSource = dv1;
            cbostate.DisplayMember = "SName";
            cbostate .ValueMember = "SId";
           
        }

        private void cboCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
        
            DataView dv1;
            //ds.ReadXml(xmlfile);
            dv1 = new DataView(ds.Tables[1]);
            string str = cboCountry.SelectedValue.ToString();
            if (str != "System.Data.DataRowView" & cboCountry.SelectedIndex != -1)
            {
                dv1.RowFilter = "Country_Id=" + cboCountry.SelectedValue.ToString();
            }
            cbostate.DataSource = dv1;
            cbostate.DisplayMember = "SName";
            cbostate.ValueMember = "SId";
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900