Skip to content

ListView - Unable to view data in the control

XMarshall XMarshall

Asked Dec 20, 2008, 11:16 PM EST3 answers
Share
LinkedInX
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());
                        }
                       
                    }

                }
          }

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    Might be because of the if close

    try this instead, are you sure that str is set to details when you are calling the method
    if (str == "DETAILS")
    0
  • 0
    Yes, I am sure that the control is coming inside the "DETAILS" section as I have debugged it. All the data is coming correctly also. Only it is not being displayed into the control. Here are control properties, least if I have done something wrong in them:
                //
                // listViewDetails
                //
                this.listViewDetails.FullRowSelect = true;
                this.listViewDetails.GridLines = true;
                this.listViewDetails.Location = new System.Drawing.Point(16, 77);
                this.listViewDetails.MultiSelect = false;
                this.listViewDetails.Name = "listViewDetails";
                this.listViewDetails.Size = new System.Drawing.Size(487, 261);
                this.listViewDetails.TabIndex = 10;
                this.listViewDetails.UseCompatibleStateImageBehavior = false;
                this.listViewDetails.View = System.Windows.Forms.View.Details;
    0
  • Bechir Bejaoui
    0
     No the code seems to be all right but never te less try differently

    Code
    ListView1.DataSource = sqlcommand.ExecuteReader(CommandBehavior.CloseConnection);
    ListView1.DataBind();
    0