0

I have a List<Indicators> where Indicators has several properties.

I have a ListView which holds selected properties of that class as a list of strings.

I want to create a DataTable from the List using only the selected properties in the listview.

So far I can create the columns for the DataTable with the selected parameters in listview. I'm stuck on how to fill each row with data from selected columns only.

the for block is where I'm stuck, I know what I have there isn't right. Any help is appreciated, thanks.

  internal DataTable ConvertToDataTableAll(List<Indicators> data)
    {

        DataTable table = new DataTable();           
        foreach (ListViewItem item in listviewFeatures.Items)
        {

            table.Columns.Add(item.Text);

            //this for block should fill the current column with data.
            for (int i = 0; i < data.Count; i++)
            {
                var row = table.NewRow();

                table.Rows.InsertAt(row, i);
                table.Rows[i][item.Text] = data.Select(x => item.Text);

            }

        }
    }

Solution with some changes to CodingYoshis suggestion. This is after adding all columns from code above and removing the for block.

        foreach(Indicators ind in data)
        {
            var row = table.NewRow();

            foreach(PropertyInfo prop in ind.GetType().GetProperties())
            {
                if (table.Columns.Contains(prop.Name))
                {
                    row[prop.Name] = prop.GetValue(ind);
                }

            }
            table.Rows.Add(row);
        }
Viraj
  • 3
  • 3
  • have you tried this? http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable – bamanow Dec 27 '16 at 16:49
  • Why don't you replace your for with: `foreach(DataRow row in data.Rows) { row.Text = data.Select(x => item.Text); table.Rows.Add(row); } ` – Phiter Dec 27 '16 at 16:51

1 Answers1

0

You need to add the columns once for the entire table, not for every row as you are doing. So it should be done like this:

var firstItem = listviewFeatures.Items.FirstOrDefault();
if (firstItem == null)
{
    // Nothing to convert to datatable so return
    return;
}

// We have items so lets initialize the table
DataTable table = new DataTable();
foreach (ColumnHeader header in listviewFeatures.Columns)
{
      table.Columns.Add(header);
}

// Now lets add the rows
foreach (ListViewItem item in listviewFeatures.Items)
{

    // Create one row per row in listview
    var row = table.NewRow();

    // Traverse the listview by each column and fill the row
    foreach (ColumnHeader header in listviewFeatures.Columns)
    {
        row[header] = item.Text;
    }

    // Add row to table. It will add it to the end.
    table.Rows.Add(row);   
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Thank you, yes I think that was my problem. However my listviewFeatures.Columns is empty. I fill listviewFeatures.Items with my selected Parameters from List . feautres.items contains the column names in string that i want to extract from list to datatable. – Viraj Dec 27 '16 at 17:49
  • Would it be easier to just convert the List with all parameters to DataTable then drop the columns I don't want? – Viraj Dec 27 '16 at 17:50
  • why would that be easier? – CodingYoshi Dec 27 '16 at 17:52
  • I don't know, my brain just isn't functioning. Appreciate the help, I'll edit my solution. – Viraj Dec 27 '16 at 18:08
  • If this answer has answered your question, then do [this](http://stackoverflow.com/help/someone-answers) please. – CodingYoshi Dec 27 '16 at 18:16