How to restrict the Combo box items
  • Hi my dear friends
    In my form i have 2 combo box the name is Source & destination combo box . i add the same binding source to both combo box. the items are Chennai,Kerala,Goa,Delhi,Kolkata.
    if i select the Chennai from the source combo box the destination combo box does not show the Chennai 
    How to write it plz tell me 
    i am waiting to receive your wonderful answer

  • 3 Comments sorted by
  • hi akwin hope you fine, its because you have same data source. You have to make at least copy of datasource.
    Check my below code.



    DataTable dt = new DataTable();
    dt.Columns.Add("CityId", typeof(int));
    dt.Columns.Add("CityName", typeof(string));

    DataRow dr = dt.NewRow();

    dr[0] = 1;
    dr[1] = "Chennai";
    dt.Rows.Add(dr);

    dr = dt.NewRow();

    dr[0] = 2;
    dr[1] = "Dehli";
    dt.Rows.Add(dr);

    dr = dt.NewRow();

    dr[0] = 3;
    dr[1] = "Mumbai";
    dt.Rows.Add(dr);

    dr = dt.NewRow();

    dr[0] = 4;
    dr[1] = "Kochin";
    dt.Rows.Add(dr);


    cmbSource.DataSource = dt;
    cmbSource.DisplayMember = "CityName";
    cmbSource.ValueMember = "CityId";

    DataTable dt2 = dt.Copy();
    cmbDestination.DataSource = dt2;
    cmbDestination.DisplayMember = "CityName";
    cmbDestination.ValueMember = "CityId";


  • thank you so much waqas
  •   I did like  this waqas any how thank you so much for your great help
     GetRepo _Grepo = new GetRepo();
            List<PickupPointMaster> _list = new List<PickupPointMaster>(); 
    private void Form1_Load(object sender, EventArgs e)
            {
                _list = _Grepo.GetAllPickupPoints();
                comboBox1.DisplayMember = "Name";
                comboBox1.ValueMember = "PickupPointMasterId";
                comboBox1.DataSource = _list;
                comboBox1.SelectedIndex = -1;
            }

            private void comboBox1_Leave(object sender, EventArgs e)
            { 
                var tolist = _list.Where(x => x.PickupPointMasterId != Convert.ToInt32(comboBox1.SelectedValue)).ToList();
                comboBox2.DisplayMember = "Name";
                comboBox2.ValueMember = "PickupPointMasterId";
                comboBox2.DataSource = tolist;
            }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Optimum Creative