C#.NET: Display Excel data under correct header in ListView
Posted on 2007-10-04
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[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?