views:

413

answers:

1

I like to be able to view datatable in windows form

I managed to get the headers only with ListView how to get the data in there

DataTable data = new DataTable();

data = EnumServices();

//create headers
foreach (DataColumn column in data.Columns)
    {
      listView_Services.Columns.Add(column.ColumnName);
    }

I just want to show now the data in there!

cheers

+3  A: 
foreach (DataRow row in data.Rows)
{
    ListViewItem item = new ListViewItem(row[0].ToString());
    for (int i = 1; i < data.Columns.Count; i++)
    {
        item.SubItems.Add(row[i].ToString());
    }
    listView_Services.Items.Add(item);
}

Update: also, if you're calling your method more than once, you need to either clear the columns collection before adding the columns, or check to see if the columns have already been added - otherwise, the number of columns will keep increasing every time you call your method.

MusiGenesis
nice, but my first row is empty !!!
Data-Base
I assume that means you have a blank row of data in your database?
MusiGenesis
Or do you mean the first *column* is empty?
MusiGenesis
now it is not! the whole fields shifted in position!
Data-Base
Try my edited version above.
MusiGenesis
Perfect :-)Thanks allot
Data-Base