繁体   English   中英

C#中的DataTable列

[英]DataTable columns in C#

我目前有这段代码,我知道如何打印行,但是我不知道如何获取列标题? 我不想使用我注释掉的解决方案,因为我想使代码通用,以便也可以将其用于其他列表。

static DataTable ConvertListToDataTable(List<List<string>> list)
{
    // New table.
    DataTable table = new DataTable();

    /* table.Columns.Add("Employee ID");
       table.Columns.Add("First Name");
       table.Columns.Add("Last Name");
       table.Columns.Add("Job Title");
       table.Columns.Add("Address");
       table.Columns.Add("City"); 
    */

    foreach(List<string> row in list) {
        table.Rows.Add(row.ToArray());
    }

    return table;
}

由于该信息完全不可用,因此无法从List<List<string>>导出列标题。 您可以按参数提供它们:

static DataTable ConvertListToDataTable(List<List<string>> list, IList<string> columnNames)
{
    DataTable table = new DataTable();
    foreach (string columnName in columnNames)
        table.Columns.Add(columnName);
    foreach (List<string> row in list)
    {
        if (row.Count != columnNames.Count)
            throw new ArgumentException(string.Format("Invalid data in list, must have the same columns as the columnNames-argument. Line was: '{0}'", string.Join(",", row)), "list");
        DataRow r =  table.Rows.Add();
        for (int i = 0; i < columnNames.Count; i++)
            r[i] = row[i];
    }
    return table;
}

如何使用:

string[] columns = { "Employee ID", "First Name", "Last Name", "Job Title", "Address", "City"};
DataTable tblEmployee = ConvertListToDataTable(employees, columns);

但是,应该使用自定义类,例如具有所有这些属性的Employee ,而不是使用List<List<string>> (或DataTable )来存储员工。 然后,您可以填充List<Employee> 这样一来,您的代码将更易于阅读和维护。

以下代码为您提供了使用System.Reflection.PropertyInfo将IEnumerable类型转换为具有动态标头的DataTable的便利。 尝试使用这个。

    public static DataTable EnumerableToDataTable<T>(IEnumerable<T> varlist)
    {
        DataTable dtReturn = new DataTable();

        // column names  
        PropertyInfo[] oProps = null;

        if (varlist == null) return dtReturn;

        foreach (T rec in varlist)
        {
            // Use reflection to get property names, to create table, Only first time, others will follow  
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;

                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }

                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }

            DataRow dr = dtReturn.NewRow();

            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }

            dtReturn.Rows.Add(dr);
        }
        return dtReturn;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM