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
           FallQueryACSToExcel
        ElseIf (strQuery = "qryCuPSQ") Then
           SpringQueryACSToExcel

---------------------

tblCustReports:

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")
                FallQueryACSToExcel(selectedyear);
            else if (itemText == "qryCuPSQ")
                SpringQueryACSToExcel(selectedyear);
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")) {
            //ConsolidateXLS
            FallQueryACSToExcel();
      } else if ((strQuery == "qryCuPSQ")) {
            SpringQueryACSToExcel();

      }


}

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

Link to home
membership
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

ASKER

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".