0

I'm just trying to get values into a datagridview selection items from a drop menu. I got it, but when I got a Item which one no contains value I wanna show a label...the example does, but then, the other drop menu loses the items...I mean, I wanna show countries and cities...I'm using C#.

    private void cmdPaisesFiltro_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbPaisesFiltro.SelectedItem == null)
            {
                cmbProvinciasFiltro.DataSource = null;
                cmbProvinciasFiltro.Items.Clear();
                return;
            }
                int id = Convert.ToInt32(((DataRowView)cmbPaisesFiltro.SelectedItem).Row["PAI_ID"]);
                DataSet dsDataFromDB = FProvincias.Filtro(id);
                if (dsDataFromDB.Tables[0].Rows.Count == 0)

                    if (dt.Rows.Count > 0)
                    {

                       lblNada.Visible = true;
                        //label
                    }


            {
                cmbProvinciasFiltro.DataSource = null;
                cmbProvinciasFiltro.Items.Clear();
                return;
            }

                cmbProvinciasFiltro.DisplayMember = "PROV_DESCRIPCION";
                cmbProvinciasFiltro.ValueMember = "PROV_ID";
                cmbProvinciasFiltro.DataSource = dsDataFromDB.Tables[0];
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    } `
Ben
  • 1,299
  • 3
  • 17
  • 37
  • 1
    it looks like you have some misplaced `{` `}`. – dub stylee Mar 31 '15 at 00:06
  • @dubstylee where?if (dt.Rows.Count > 0) { lblNada.Visible = true; //label } else { lblNada.Visible = false; } – Lalooh Flores Cruz Mar 31 '15 at 00:19
  • There are no {} for your line `if (dsDataFromDB.Tables[0].Rows.Count == 0)` and then below the `if (dt.Rows.Count > 0)` statement, there is a pair of {} by themselves where you clear the `DataSource` and `return;`. – dub stylee Mar 31 '15 at 00:21
  • 1
    Looks like a repeat of the `if` at the top of your code, so it could just be a copy/paste error when you were posting the question. Also, the `else { lblNada.Visible = false; }` does not exist in your original post. – dub stylee Mar 31 '15 at 00:23
  • @dubstylee I got it like { if (dsDataFromDB.Tables[0].Rows.Count == 0) { lblNada.Visible = true; //label }.... but the other drop menu doesnt show nothing – Lalooh Flores Cruz Mar 31 '15 at 00:48
  • The 1st return is always executed and nothing after ever. Fix that and show the corrected code! Also: I see where you show the Label but not where you hide it.. – TaW Mar 31 '15 at 08:09

1 Answers1

0

If I'm understanding your question and code correctly, you would want the following:

    private void cmdPaisesFiltro_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbPaisesFiltro.SelectedItem == null)
            {
                cmbProvinciasFiltro.DataSource = null;
                cmbProvinciasFiltro.Items.Clear();
                return;
            }

            int id = Convert.ToInt32(((DataRowView)cmbPaisesFiltro.SelectedItem).Row["PAI_ID"]);
            DataSet dsDataFromDB = FProvincias.Filtro(id);
            if (dsDataFromDB.Tables[0].Rows.Count == 0)
            {
               // Selection contains no values in the database, show the label         
               lblNada.Visible = true;

               //label
            }
            else
            {
                // Selection contains values in the database, hide the label
                lblNada.Visible = false;

                cmbProvinciasFiltro.DisplayMember = "PROV_DESCRIPCION";
                cmbProvinciasFiltro.ValueMember = "PROV_ID";
                cmbProvinciasFiltro.DataSource = dsDataFromDB.Tables[0];
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Shar1er80
  • 9,001
  • 2
  • 20
  • 29