csharp Remove Empty rows in datatable

 1 //20120803 Geovin Du
 2                 //塗聚文 締友計算機信息技術有限公司
 3                 DataTable dt = new DataTable();
 4                 dt=agreementMonthlySalaryListBLL.SelectImportExceltoDataGridView(fileUrl, sheet).Copy();
 5 
 6                 if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0))
 7                 {
 8                     List<System.Data.DataRow> removeRowIndex = new List<System.Data.DataRow>();
 9                     int RowCounter = 0;
10                     foreach (System.Data.DataRow dRow in dt.Rows)
11                     {
12                         for (int index = 0; index < dt.Columns.Count; index++)
13                         {
14                             if (dRow[index] == DBNull.Value)
15                             {
16                                 removeRowIndex.Add(dRow);
17                                 break;
18                             }
19                             else if (string.IsNullOrEmpty(dRow[index].ToString().Trim()))
20                             {
21                                 removeRowIndex.Add(dRow);
22                                 break;
23                             }
24                         }
25                         RowCounter++;
26                     }
27                     // Remove all blank of in-valid rows
28                     foreach (System.Data.DataRow rowIndex in removeRowIndex)
29                     {
30                         dt.Rows.Remove(rowIndex);
31                     }
32                 }
33                 dataGridView1.DataSource = null;
34                 set.SetDataGridViewBindingSourceNavigatorBinds(dataGridView1,dt , bindingSource1, bindingNavigator1);

 

你可能感兴趣的:(Datatable)