ListView - Unable to view data in the control

Dec 21 2008 4:16 AM
Hi,

I am an newbie in C#. I am entering data in an ListView control after retrieving data from the SQL Sever database using OleDbConnection, OleDbCommand and OleDbDataReader classes. The code is given below. Pls let me know where I am going wrong?? I am using VS2005 and the code does not throws any exceptions, also the data is getting retrieved correctly, but only not being populated in the ListView control.

 public void DisplayRecords(OleDbDataReader rows, string str)
        {
            listViewDetails.Clear();
            try
            {
                if (str.CompareTo("DETAILS") == 0)
                {
                    int nCount = rows.FieldCount;  //nCount returns 7 (fields)
                    ListViewItem lvi = new ListViewItem();
                    for (int i = 0; i < nCount; i++)
                    {     //Adding colums headers
                        ColumnHeader header = new ColumnHeader();
                        header.Width = 100;
                        header.Text = rows.GetName(i);
                        listViewDetails.Columns.Add(header.Text);

                    }

                   //Data population
                    while (rows.Read() == true)
                    {
                        lvi = listViewDetails.Items.Add(rows.GetValue(0).ToString());
                        for (int i = 1; i < nCount; i++)
                        {
                            lvi.SubItems.Add(rows.GetValue(i).ToString());
                        }
                       
                    }

                }
          }

Answers (3)