This is a migrated thread and some comments may be shown as answers.

Problem deteting selecteditem text

1 Answer 67 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Houssem
Top achievements
Rank 1
Houssem asked on 25 Jan 2021, 04:02 PM

Hello, I am trying to get data with raddropdownlist and in case the item value selected equals to "nouveau_scénario" it will open automaticly another form else messagebox will be showen but nothing happen in both cases.

I developed this function to get data from SQLSERVER database (this works correctly ) :

        private void Combobox_scénario1()
        {        
            string constr = @"Data Source=****;Initial Catalog=****;User ID=****;Password=****";
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter("SELECT [LIBELLE],[OID] FROM [AffecAnalytique].[dbo].[DESCRIPTIF] order by [DATEMODIF] DESC", con))
                {
                   using (SqlConnection con = new SqlConnection(constr))
             {
                 using (SqlDataAdapter sda = new SqlDataAdapter("SELECT [LIBELLE],[OID] FROM [AffecAnalytique].[dbo].[DESCRIPTIF] order by [DATEMODIF] DESC", con))
                 {
                     //Fill the DataTable with records from Table.
                     DataTable dt = new DataTable();
                     sda.Fill(dt);
                     
                     //Insert the Default Item to DataTable.
                     DataRow row = dt.NewRow();
                     radDropDownListElement2.Text = "Choix du Scénario";
                     row[0] = "Nouveau_scénario";
                     dt.Rows.InsertAt(row, 0);

                     //Assign DataTable as DataSource.
                     radDropDownListElement2.DataSource = dt;
                     radDropDownListElement2.DisplayMember = "LIBELLE";
                     radDropDownListElement2.ValueMember = "OID";
                }
            }
        }

 

here the function of selected index of raddropdownlist :

         private void radDropDownListElement2_Click(object sender, EventArgs e)
         {
             Console.WriteLine("fef :"+radDropDownListElement2.SelectedValue);
             if (string.IsNullOrEmpty(textBox3.Text) || textBox3.Text == "System.Data.DataRowView")
             {
                 MessageBox.Show(" Veuillez sélectionner un scénario ");
             }
             else if (radDropDownListElement2.Text == "Nouveau_scénario")
             {
                 this.IsMdiContainer = true;
                 Nouveau_Scenario = new Form_Nouveau_Scénario();
                 Nouveau_Scenario.MdiParent = this;
                 Nouveau_Scenario.Show();
             }
             else
             {
                 string message1 = radDropDownListElement2.SelectedValue.ToString();
                 string message2 = radDropDownListElement2.Text;
                 textBox3.Text = message2;
                 textBox4.Text = message1;

             }
         }

1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 26 Jan 2021, 10:50 AM

Hi, Houssem,

I would like to recommend you to use the SelectedIndexChange event. 

You can refactor your code in this way:

        radDropDownList2.SelectedIndexChanged += RadDropDownList2_SelectedIndexChanged;
        ....

        private void RadDropDownList2_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            Console.WriteLine("fef :" + radDropDownList2.SelectedValue);
            if (radDropDownList2.SelectedIndex == -1)//string.IsNullOrEmpty(textBox3.Text) || textBox3.Text == "System.Data.DataRowView")
            {
                MessageBox.Show(" Veuillez sélectionner un scénario ");
            }
            else
            if (radDropDownList2.SelectedIndex != -1 && radDropDownList2.SelectedItem.Text == "Nouveau_scénario")
            {
                this.IsMdiContainer = true;
                Nouveau_Scenario = new Form_Nouveau_Scénario();
                Nouveau_Scenario.MdiParent = this;
                Nouveau_Scenario.Show();
            }
            else
            {
                string message1 = radDropDownList2.SelectedValue.ToString();
                string message2 = radDropDownList2.Text;
                textBox3.Text = message2;
                textBox4.Text = message1;

            }
        }

I hope this helps.

Regards,
Peter
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DropDownList
Asked by
Houssem
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or