csharp DataTable and DataGridView delete a Row

 1                     //DataTable 得到標題
 2                     //締友計算機技術有限公司 塗聚文
 3                     //foreach (DataColumn column in dt.Columns)
 4                     //{
 5                     //    selectDt.Columns.Add(column.ColumnName, column.DataType);
 6                     //}
 7                     //DataTable 移動行 delete或 Remove 可以移除,但會出現問題 塗聚文注 20120808
 8                     //foreach (System.Data.DataRow rowIndex in removeRowIndex)
 9                     //{
10                     //    dt.Rows.Remove(rowIndex);
11                     //} 
12                     //foreach (DataGridViewRow item in this.dataGridView1.SelectedRows) 
13                     //{ 
14                     //    dataGridView1.Rows.RemoveAt(item.Index); 
15                     //} 
16 
17 
18                     //DataGridView指定移除行
19                     List<DataGridViewRow> removeRowIndex = new List<DataGridViewRow>();
20                     for (int i = 0; i < dataGridView1.Rows.Count; i++)
21                     {
22                         if (!list.Contains(dataGridView1.Rows[i].Cells[0].Value.ToString()))
23                         {
24                             removeRowIndex.Add(dataGridView1.Rows[i]);
25                         }
26                     }
27                     //DataGridView選擇移除
28                     //foreach (DataGridViewRow row in dataGridView1.SelectedRows)
29                     //{
30                     //    dataGridView1.Rows.Remove(row);
31                     //}
32 
33                     foreach (DataGridViewRow dr in removeRowIndex)
34                     {
35 
36                         dataGridView1.Rows.Remove(dr);
37 
38                     }

 

posted @ 2012-08-09 16:58  ®Geovin Du Dream Park™  阅读(376)  评论(0编辑  收藏  举报