tags:

views:

35

answers:

2

Hi All,

First of all thanks to all, now I am getting the dropdown value change on the selection of first dropdwon. PFB the source code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cmbpackage.Items.Add("---Please Select---");
            cmbpackage.Items.Add("HR");
            cmbpackage.Items.Add("Test");
            cmbpackage.Items.Add("DEV");
        }    
        private void cmbpackage_SelectedIndexChanged(object sender, EventArgs e) 
     { 
        string selectedValue = cmbpackage.SelectedIndex.ToString();             

        if (selectedValue == "1") 
        { 
            cmbmodule.Items.Add("ModuleHR1"); 
            cmbmodule.Items.Add("ModuleHR2"); 
            cmbmodule.Items.Add("ModuleHR3"); 

        } 

        else if (selectedValue == "2") 
        { 
            cmbmodule.Items.Add("ModuleTest1"); 
            cmbmodule.Items.Add("ModuleTest2"); 
            cmbmodule.Items.Add("ModuleTest3"); 
        } 

        else 
        { 
            cmbmodule.Items.Add("ModuleDEV1"); 
            cmbmodule.Items.Add("ModuleDEV2"); 
            cmbmodule.Items.Add("ModuleDEV3"); 
        } 

    } 

} 
}

Now I want to make index 0. I mean "Please Select" will load at the time of page load only and not by clicking on "cmbpackage" dropdown.

Also I want to change the drop dwon values of "cmbmodule" based on selections on the cmbpackage dropdown. Currently its changing but all the values are getting stored in the "cmbmodule" dropdown. I want if I select "HR" only ModuleHR1, ModuleHR2, ModuleHR3 will display and same for rest "Test" and "DEV".

+1  A: 

What you want to do is

cmbmodule.Items.Clear();

within cmbpackage_SelectedIndexChanged so that it clears all existing values and based on your logic here, you add exactly only what you need to show based on the currently selected value.

InSane
Right if I will use "cmbmodule.Items.Clear()", it will clear all my records.
sauravinfy
What I want how can if I select "HR" only ModuleHR1, ModuleHR2, ModuleHR3 will display, same way for "Test" and "DEV", but again if I select "HR" after selecting "Test" or "DEV", it has to show only ModuleHR1, ModuleHR2, ModuleHR3. How top do this ?
sauravinfy
@sauravinfy - i just realised that you have 2 separate dropdownlist - package and combo. Since you are clearing our combo and not package, as long as you do that before the `if (selectedValue == "1")` part of your logic, then it should work just fine
InSane
Thanks it worked..
sauravinfy
A: 

Right if I will use "cmbmodule.Items.Clear()", it will clear all my records.

What I want how can if I select "HR" only ModuleHR1, ModuleHR2, ModuleHR3 will display, same way for "Test" and "DEV", but again if I select "HR" after selecting "Test" or "DEV", it has to show only ModuleHR1, ModuleHR2, ModuleHR3. How top do this ?

sauravinfy
@saurav - if you are commenting on an answer, its usually put in only as a comment and not as an "answer" which is what this is
InSane
I will take care from the next time onwards
sauravinfy