Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to select a row from a listbox and get the proper value using C# with VS 2005?

I am converting an application from Access 2003 VBA to a C# Windows application using VS2005.

How would you convert the following Access VBA snippet to C#?

List Box: lstCustRpts  
Row Source SELECT tblCustReports.ReportID, tblCustReports.ReportName, tblCustReports.ReportDescription FROM tblCustReports ORDER BY ReportID;

For Each vnt In lstCustRpts.ItemsSelected
        strQuery = lstCustRpts.Column(1, vnt)
        If (strQuery = "qryCuPFQ") Then
        ElseIf (strQuery = "qryCuPSQ") Then



ReportID  ReportName        ReportDescription
-----------  ----------------       ---------------------------
3                 qryCuPFQ                 ACS Fall Report
4                 qryCuPSQ                 ACS Spring Report

I tried the following C# code but I get the same value

itemText = "qryCuPFQ" for each row I select from the listbox
string selectedyear = (string)this.comboBox1.SelectedItem;
            DataRowView drv = (DataRowView)listBox3.Items[0];
            string itemText = drv.Row["ReportName"].ToString();
            if (itemText == "qryCuPFQ")
            else if (itemText == "qryCuPSQ")
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Have you tried like this...

foreach ( vnt in lstCustRpts.ItemsSelected) {
      strQuery = lstCustRpts.Column(1, vnt);
      if ((strQuery == "qryCuPFQ")) {
      } else if ((strQuery == "qryCuPSQ")) {



EDIT : Please ignore answer
Avatar of dungla
Flag of Viet Nam image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zimmer9


I have set the SelectionMode property to "One" for the ListBox which is my intent. But I must be doing something wrong in my code because NO MATTER which ReportName I select from the ListBox, the return value is the 1st ReportName, which is "qryCuPFQ".