How to Perform IF function when selecting Combobox item?

Asked

Viewed 1,578 times

2

I wanted to know how to make one if function only when the combobox have any item selected. I am making a program in windows Forms and I need a if to limit the program to only do that function when the combobox contain something selected. This is the code I have to limit:

 if () {
    var adapter = new OleDbDataAdapter("SELECT * FROM [" + comboBox1.SelectedText + "$]", conexao);
    var ds = new DataSet();
    adapter.Fill(ds, comboBox1.SelectedText + "$");
    DataTable data = ds.Tables[comboBox1.SelectedText + "$"];

    foreach (DataColumn dc in data.Columns)
    {
         comboBox2.Items.Add(dc.ColumnName);
         comboBox3.Items.Add(dc.ColumnName);
         comboBox4.Items.Add(dc.ColumnName);
         comboBox5.Items.Add(dc.ColumnName);
    }
 }

I fill out the combobox1 with this code:

using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
                    IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
                    reader.IsFirstRowAsColumnNames = true;
                    result = reader.AsDataSet();
                    comboBox1.Items.Clear();

                    foreach (DataTable dt in result.Tables) comboBox1.Items.Add(dt.TableName);
                    reader.Close();

                    string ConecçãoDB = ConfigurationManager.ConnectionStrings["ConecçaoDB"].ConnectionString;
                    string Table = ConfigurationManager.AppSettings["table"];

                    string ssqltable = Table;

                    string ssqlconnectionstring = ConecçãoDB;

                    filename = ofd.FileName;
                     MessageBox.Show(Convert.ToString(filename));
                    var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";

                    var conexao = new System.Data.OleDb.OleDbConnection(connectionString);

                    var sql = "SELECT * FROM [PARAC1$]";
                    string sclearsql = "delete from " + ssqltable;
}
}
  • Question too broad, vague is Web is Forms is in which of them? lack to give a contextualized and put more information .

  • @Virgilionovic already updated my question , I apologize .

  • 1

    Put the current code! also this helps to clear more ...

  • comboBox1.Selectedtext is named after spreadsheets?

  • 1

    @Virgilionovic Yes ,has .

2 answers

3


if (comboBox1.Items.Count > 0) comboBox1.SelectedIndex = 0; // posiciona no primeiro item
if (string.IsNullOrEmpty(comboBox1.Text) == false) 
{
    var adapter = new OleDbDataAdapter("SELECT * FROM [" 
                         + comboBox1.Text+ "$]", conexao);
    var ds = new DataSet();
    adapter.Fill(ds, comboBox1.Text+ "$");
    DataTable data = ds.Tables[comboBox1.Text + "$"];

    foreach (DataColumn dc in data.Columns)
    {
        comboBox2.Items.Add(dc.ColumnName);
        comboBox3.Items.Add(dc.ColumnName);
        comboBox4.Items.Add(dc.ColumnName);
        comboBox5.Items.Add(dc.ColumnName);
    }
}
  • I do not know why but so does not write in the other combobox the names of the columns

  • If you have already debugged the code @Pedroazevedo ??? with breakpoint?

  • Yes, it just doesn’t do anything

  • made an issue @Pedroazevedo is Text take a look!

  • It does nothing I tried to put in the code a message box and it does not appear

  • How do you fill it @Pedroazevedo put in your question.

  • How do I fill in what ?

  • How do you upload the information to comboBox1 ???

  • I’ve already updated .

  • @Pedroazevedo you updated the way I did in the reply?

  • Yeah, it still didn’t work

  • It must be a local problem of difficult reproduction, you know if it enters in this if() using breakpoint and F11 ???

  • He passes the if, never gets in

Show 9 more comments

3

You want to perform certain function when some item of your Combobox is selected right?

At the event Selectedindexchanged of Combobox it is possible to perform such action, see.

private void meuComboBox_SelectedIndexChanged(object sender, EventArgs e){
   if (meuComboBox.SelectedIndex >= 0)
      //Executa sua função "if" aqui
   }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.