创建Dataview列表时出错

时间:2016-08-31 08:37:43

标签: c# dataset dataview

我正在尝试创建一个数据视图列表,稍后将在代码中用作函数的参数。我收到此错误"对象引用未设置为对象的实例。"我不知道如何将dataview列表初始化为null

        List<DataView> dvTablesLookup = null;
        List<DataTable> dtTablesLookup = null;

        // Creating Data View
        for (int i=0; i < datatablesLookup.Count; i++ )
        {
            dvTablesLookup[i] = new DataView(datatablesLookup[i]);
            dvTablesLookup[i].Sort = sortLookup;
            dtTablesLookup[i] = dvTablesLookup[i].ToTable();
        }

2 个答案:

答案 0 :(得分:1)

假设datatablesLookup是List()

var datatablesLookup = new List<DataTable>();
List<DataView> dvTablesLookup = null;
List<DataTable> dtTablesLookup = null;

dvTablesLookup = datatablesLookup.Select(dt => new DataView(dt)).ToList();
dvTablesLookup.ForEach(x => x.Sort = sortLookup);
dtTablesLookup = dvTablesLookup.Select( dv => dv.ToTable()).ToList();

答案 1 :(得分:0)

尝试:

    List<DataView> dvTablesLookup = new List<DataView>();
    List<DataTable> dtTablesLookup = new List<DataTable>();

    // Creating Data View
    for (int i=0; i < datatablesLookup.Count; i++ )
    {
        DataView tempdv = new DataView(datatablesLookup[i]);
        tempdv.Sort = sortLookup;
        dvTablesLookup.Add(tempdv);                
        dtTablesLookup.Add(tempdv.ToTable());
    }