Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my source code where i populated my combobox from excel sheet sheet name but
i am unable to get value of index on the same event

 private void button5_Click(object sender, EventArgs e)
       {
           OpenFileDialog op = new OpenFileDialog();
           if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               textinput.Text = op.FileName;
           }
Excel.Application xlApp;
           Excel.Workbook xlWorkBook;
           xlApp = new Excel.Application();
           xlWorkBook = xlApp.Workbooks.Open(textinput.Text, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
           object misValue = System.Reflection.Missing.Value;
 List<string> sheetname = new List<string>();

           foreach (Microsoft.Office.Interop.Excel.Worksheet wSheet in xlWorkBook.Worksheets)
           {
               sheetname.Add(wSheet.Name);
           }
           comboBox2.DataSource = sheetname;

           xlWorkBook.Close(true, misValue, misValue);
           xlApp.Quit();
           releaseObject(xlWorkBook);
           releaseObject(xlApp);
           }


when i am trying to get combobox2.selectindex by changing combo drop down ,always getting 0 as index value on the same event ,is there any way to get select index value
plz help.my combobox is populating the sheet name correctly,but not getting the index value???
Posted
Comments
Sergey Alexandrovich Kryukov 4-Apr-15 22:45pm    
To start with, specify what do you mean by ComboBox. Which one? Full type name, please. Is it System.Windows.Forms.ComboBox? Anything else?
—SA
Afzaal Ahmad Zeeshan 4-Apr-15 22:46pm    
Where are you getting the SelectedIndex value? I don't see anything like that here. Just that you're setting the DataSource to sheetname only.

1 solution

Try the following -
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cmb = (ComboBox)sender;
    int selectedIndex = cmb.SelectedIndex;
    int selectedValue = (int)cmb.SelectedValue;

    ComboboxItem itm= (ComboboxItem)cmb.SelectedItem;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900