How to execute IF function when selecting item from ComboBox?

3

I wanted to know how to make a if work only when combobox has any items selected. I'm doing a program in windows forms and I need a if to limit the program to only doing that function when combobox contains 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 populate 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;
}
}
    
asked by anonymous 12.10.2017 / 15:57

2 answers

4
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);
    }
}
    
12.10.2017 / 16:08
4

You want to run a certain function when some item from your ComboBox is selected right?

In the SelectedIndexChanged event of the ComboBox it is possible to perform such an action, see.

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