1

我一直在做一个项目,该项目需要两个 csv 文件作为输入,并在 dataTable 中导入。现在我想加入这两个,最后将加入的一个显示给 dataGridView。

该程序运行良好,直到导入部分。

到目前为止,我创建的代码没有显示任何错误,但它不起作用,我的意思是,我的目标数据网格视图仍然是空白的。

请遵守以下代码并提供一些可行的解决方案。此致。

PS:我正在尝试在这里进行完全外部连接。

var tbl1 = tb1;
var tbl2 = tb2;
var res1 = from t2 in tbl2.AsEnumerable()
           join t1 in tbl1.AsEnumerable() on t2["BBL"] equals t1["BBL"] into g
           from t1 in g.DefaultIfEmpty()
           where t1 == null
           select t2;

dataGridView3.DataSource = res1;
4

2 回答 2

0

您必须创建列或复制 res1 的架构。

例子:

res1.columns.Add("RowError",typeof(string));
于 2012-05-30T15:41:29.447 回答
0

这里的问题是,它的非索引属性res1的集合被呈现为列。DataRow

解决方案 1

您可以选择以行内容作为其属性的匿名对象类型。

例如,

var tbl1 = tb1;
var tbl2 = tb2;
var res1 = from t2 in tbl2.AsEnumerable()
           join t1 in tbl1.AsEnumerable() on t2["BBL"] equals t1["BBL"] into g
           from t1 in g.DefaultIfEmpty()
           where t1 == null
           select new { Amount = t2["amount"], Payee = t2["payee"] };

其中amountpayee是 中的两列tb2

如果设置AutoGenerateColumnsdataGridView3true则此匿名类的属性名称(即AmountPayee)将用作列名。

解决方案 2

正如您在评论中所说,您想显示所有列,所以这是另一个不太优雅的解决方案(请保持您的 Linq 表达式不变):

foreach (DataColumn dCol in tbl1.Columns)
{
    dataGridView1.Columns.Add(dCol.ColumnName, dCol.Caption);
}

foreach (var s in res1)
{
    foreach (var item in res1)
    {
        dataGridView1.Rows.Add(item.ItemArray);
    }
}

dataGridView3.DataSource = res1;如果你继续使用这种方法,你应该删除。

于 2012-05-30T15:59:02.310 回答