2012-04-16 39 views
0

我正在使用以下代码从DataTable返回n个行到另一个DataTable对象。当第一个数据表记录来自数据库时,它工作正常,但是当我mannualy在数据表中添加记录时,它会崩溃并显示一条消息no rows。 我已经打垮了,发现currentTable.DefaultView.ToTable().Rows.Cast<DataRow>()这将返回空值。 我写了下面的代码从DataTable对象从数据表对象返回n个数据表格格式的行

currentTable = currentTable.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable(); 

我不知道它为什么当我manualy创建数据表和工作正常时,记录来自DataReader的数据表来选择坠毁N条记录。

我使用下面的代码来创建数据表手动

DataTable tblCurrent = new DataTable(); 
//For Adding columns 
for (int i = 0; i < fieldCol.Length; i++) 
    tblCurrent.Columns.Add(string.Format("Column{0}", i + 1)); 

//for adding rows. 
for (int i = (count + globalClass.sourceConnectionInfo.RowsToSkip); i < lineRows.Length; i++) 
{ 
    DataRow newRow = tblCurrent.NewRow(); 
    for (int j = 0; j < fieldCol.Length; j++) 
     newRow[j] = fieldCol[j]; 
    tblCurrent.Rows.Add(newRow); 
} 


//Then for selecting n records 
if (currentTable.Rows.Count > 0) 
    tblCurrent = tblCurrent.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable(); 
+0

什么是崩溃堆栈,你会得到什么?它有没有说'InvalidCastException'? – dasblinkenlight 2012-04-16 10:02:13

+0

这有帮助吗? :http://stackoverflow.com/questions/425424/databinding-to-a-programmaticly-created-datatable – 2012-04-16 10:02:33

+0

@dasblinkenlight它给出以下消息“源不包含DataRows”。 – Awadhendra 2012-04-16 10:05:57

回答

0

检查下面的代码片段,它是在没有任何错误做工精细。

void Main() 
    { 
    DataTable dt = new DataTable(); 
     if (dt.Columns.Count == 0) 
       { 
        dt.Columns.Clear(); 
        dt.Columns.AddRange(
         new DataColumn[] 
         { 
          new DataColumn("TStamp", typeof(DateTime)), 
          new DataColumn("C1", typeof(double)), 
          new DataColumn("C2", typeof(double)), 
          new DataColumn("C3", typeof(double)) 
          //new DataColumn("Unit", typeof(string)) 
         } 
         ); 
       } 

        Random ovValue = new Random(); 
       for (int i = 0; i < 10; i++) 
       { 
        DataRow dnew = dt.NewRow(); 
        dnew["TStamp"] = DateTime.Now.AddMonths(i); 
        dnew["C1"] = ovValue.Next(1, 100); 
        dnew["C2"] = ovValue.Next(1, 100); 
        dnew["C3"] = ovValue.Next(1, 100); 
        dt.Rows.Add(dnew); 
       } 
       try 
       { 
       int numberOfRecord = 100; 
       int count = dt.Rows.Count; 
       if(numberOfRecord <= count) 
       { 
       dt = dt.DefaultView.ToTable().Rows.Cast<DataRow>().Take(numberOfRecord).CopyToDataTable(); 
       } 
       else 
       { 
       /// No Erroe cause if you select more tested at LinqPad 
       dt = dt.DefaultView.ToTable().Rows.Cast<DataRow>().Take(100).CopyToDataTable(); 
       } 
       } 
       catch(Exception ex) 
       { 
       Console.WriteLine("Error"); 
       } 

    foreach(DataRow r in dt.Rows) 
    { 
    Console.WriteLine(r["TStamp"].ToString()); 
    } 

    } 

检查你的下面的代码块,可能是这使得数据表中没有行。在选择之前将dt.Rows.Clear();放在我的代码块中会导致错误。

//for adding rows. 
for (int i = (count + globalClass.sourceConnectionInfo.RowsToSkip); i < lineRows.Length; i++) 
{ 
    DataRow newRow = tblCurrent.NewRow(); 
    for (int j = 0; j < fieldCol.Length; j++) 
     newRow[j] = fieldCol[j]; 
    tblCurrent.Rows.Add(newRow); 
}