[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1768
  • Last Modified:

C#.NET: Display Excel data under correct header in ListView

Hi

I am writing a program in C#.NET (Visual Studio 2005).

I am trying to open an Excel file and display the contents in a ListView. I open the file and load it into a DataTable called MyDT

I then create the coloum headers in ListView like this:

      for (int k = 0; k < NumCol; k++)
      {
        listView1.Columns.Add(MyDT.Rows[1][k].ToString(), 100, HorizontalAlignment.Center);
      }

This works fine and all the coloum header are created.

My problem is when I try and fill in the remaining data. I try and do it like this:

      for (int i = 2; i < NumRow; i++)
      {
         for (int m = 0; j < NumCol; m++)
         {
            LVI = listView1.Items.Add(MyDT.Rows[i][m].ToString());
            progressBar1.Value = i;
         }
      }

What happens is that all the data in the Excel file are displayed vertically in the first coloum and not row by row under the correct coloum header.

Can anyone help with this? How do I get the data to be displayed under the correct coloum headers?

Regards
Søren Augustesen
0
poultarp
Asked:
poultarp
2 Solutions
 
nayernaguibCommented:
Inside the inner loop, you need to create an object of type ListViewItem, and populate its SubItems collection with column values, and then add the item to the ListView control. See the following example:

  http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview.items.aspx

_______________

  Nayer Naguib
0
 
Pratima PharandeCommented:
http://www.ondotnet.com/pub/a/dotnet/2002/10/28/listview.html

I think you need to do somthing like this

 for (int i = 2; i < NumRow; i++)
      {
         ListViewItem LVI = new ListViewItem(MyDT.Rows[i][0].ToString());
        for (int m = 1; j < NumCol; m++)
         {
           LVI .SubItems.Add(MyDT.Rows[i][m].ToString());
            progressBar1.Value = i;
         }
      }
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now