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

Posted on 2007-10-04
Last Modified: 2013-12-17

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?

Søren Augustesen
Question by:poultarp
    LVL 14

    Accepted Solution

    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:


      Nayer Naguib
    LVL 39

    Assisted Solution

    by:Pratima Pharande

    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;

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now