如何转换List< T>仅具有选定参数的数据表 [英] How can I convert a List<T> to DataTable with only selected parameters

查看:72
本文介绍了如何转换List< T>仅具有选定参数的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<Indicators>,其中指标具有多个属性.

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

我有一个ListView,它以字符串列表的形式保存该类的选定属性.

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.

到目前为止,我可以在列表视图中使用选定的参数为DataTable创建列.我坚持只用选定列中的数据填充每一行.

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.

for块是我被卡住的地方,我知道我所拥有的不正确.感谢您的任何帮助.

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);

            }

        }
    }

对CodingYoshis建议进行了一些更改的解决方案. 这是在上面的代码中添加了所有列并删除了for块之后.

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);
        }

推荐答案

您需要为整个表格添加一次列,而不是为每一行添加一次.所以应该这样:

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);   
}

这篇关于如何转换List&lt; T&gt;仅具有选定参数的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆