尽管为它们赋值,DataRows仍然为null

时间:2014-02-26 19:36:10

标签: c# mysql sql linq datatable

我正在尝试学习如何使用Linq将两个数据表连接成一个。我的linq查询工作正常,我可以看到它的预期值。但是,当我循环linq结果,并将值分配给新创建的数据行并将该行添加到新数据表时,行将显示为空。

这是我的代码:

private void btnCombine_Click(object sender, EventArgs e)
{

    var innerJoinQuery =
        from strRow in StrDataTable.AsEnumerable()
        join intRow in IntDataTable.AsEnumerable() 
            on strRow.Field<int>("IntID") equals intRow.Field<int>("ID")
        select new { 
            IntOne = intRow.Field<int>("FirstNum"), 
            IntTwo = intRow.Field<int>("SecondNum"), 
            StrOne = strRow.Field<string>("FirstStr"),
            StrTwo = strRow.Field<string>("SecondStr"), 
            StrThree = strRow.Field<string>("SecondStr")
        };

    DataTable newTable = new DataTable();
    newTable.Columns.Add("IntOne");
    newTable.Columns.Add("IntTwo");
    newTable.Columns.Add("FirstStr");
    newTable.Columns.Add("SecondStr");
    newTable.Columns.Add("ThirdStr");

    newTable.Columns["IntOne"].DataType = System.Type.GetType("System.String");
    newTable.Columns["IntTwo"].DataType = System.Type.GetType("System.String");
    newTable.Columns["FirstStr"].DataType = System.Type.GetType("System.String");
    newTable.Columns["SecondStr"].DataType = System.Type.GetType("System.String");
    newTable.Columns["ThirdStr"].DataType = System.Type.GetType("System.String");

    foreach (var row in innerJoinQuery)
    {
        DataRow rowToAdd = newTable.NewRow();
        rowToAdd.ItemArray[0] = row.IntOne.ToString();
        rowToAdd.ItemArray[1] = row.IntTwo.ToString();
        rowToAdd.ItemArray[2] = row.StrOne.ToString();
        rowToAdd.ItemArray[3] = row.StrTwo.ToString();
        rowToAdd.ItemArray[4] = row.StrThree.ToString();

        newTable.Rows.Add(rowToAdd);
    }

    dataGridView3.DataSource = newTable;
}

2 个答案:

答案 0 :(得分:1)

DataRow.ItemArray属性与单个值一起使用不起作用 - 而是创建object[]数组,然后将整个事物设置为.ItemArray属性。有关其他示例,请参阅此MSDN page

foreach (var row in innerJoinQuery)
{
    DataRow rowToAdd = newTable.NewRow();

    object[] items = new object[] {
        row.IntOne.ToString(),
        row.IntTwo.ToString(),
        row.StrOne.ToString(),
        row.StrTwo.ToString(),
        row.StrThree.ToString()
    };

    rowToAdd.ItemArray = items;

    newTable.Rows.Add(rowToAdd);
}

或者,直接使用DataRow 索引器,它适用于各个列:

rowToAdd[0] = row.IntOne.ToString();
rowToAdd[1] = row.IntTwo.ToString();
rowToAdd[2] = row.StrOne.ToString();
rowToAdd[3] = row.StrTwo.ToString();
rowToAdd[4] = row.StrThree.ToString();

此外,在创建列时,有一个构造函数采用可以为您节省一些代码的类型。您的前两列类型不匹配。

newTable.Columns.Add("IntOne", typeof(int));
newTable.Columns.Add("FirstStr", typeof(string));

答案 1 :(得分:0)

前两个值似乎是整数:

IntOne = intRow.Field<int>("FirstNum"), 
IntTwo = intRow.Field<int>("SecondNum"), 

但是,您为列分配的DataType是字符串:

newTable.Columns["IntOne"].DataType = System.Type.GetType("System.String");
newTable.Columns["IntTwo"].DataType = System.Type.GetType("System.String");

将那些更新为int's并查看是否能解决它:

newTable.Columns["IntOne"].DataType = System.Type.GetType("System.Int32");
newTable.Columns["IntTwo"].DataType = System.Type.GetType("System.Int32");

rowToAdd.ItemArray[0] = row.IntOne;
rowToAdd.ItemArray[1] = row.IntTwo;

您可能还需要为列提供DataPropertyName

newTable.Columns["IntOne"].DataPropertyName = "FirstNum";
newTable.Columns["IntTwo"].DataPropertyName = "SecondNum";
...

确保AutoGenerateColumns值设置为false

dataGridView3.AutoGenerateColumns = false;