2013-01-16 43 views
0

我需要獲取DataTable中所有單元格的數量,我該怎麼做?我需要這個來驗證我的2表格在將數據插入數據庫之前具有相同的單元數量。如何迭代DataTable單元格

+1

'VAR NCELLS = dt.Rows.Count * dt.Columns.Count;' – digEmAll

+0

@CodeIgnoto是的,我使用open.xml讀取Excel工作表,但這個sdk忽略空行。所以我想檢查excel文件數據表是否與數據庫數據表具有相同的單元數。 – Lahib

回答

2

試試這個:

DataTable dt1 = new DataTable(), dt2 = new DataTable(); 

    // define and populate your tables 

    int count1 = dt1.Columns.Count * dt1.Rows.Count; 
    int count2 = dt2.Columns.Count * dt2.Rows.Count; 

    bool verified = count1 == count2; 
2
DataTable tbl = new DataTable(); 
foreach (DataRow row in tbl.Rows) 
{ 
    foreach (DataColumn col in tbl.Columns) 
    { 
    object cellData = row[col]; 
    } 
} 
+1

這不會像OP所要求的那樣獲得DataTable中所有單元的計數,它只是循環單元。 –

+0

是的,我知道,我回答瞭如何遍歷DataTable單元格 – kamote