2012-04-20 86 views
1
 string[] column0Array = new string[dataGridView1.Rows.Count]; 
     string[] column1Array = new string[dataGridView1.Rows.Count]; 


     int k = 0; 

     List<DataGridViewRow> rows = null; 
     for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     { 
      if (rows != null) 
      if (i % 4 == 0) 
       rows.Add(dataGridView1.Rows[i]); 

      foreach (DataGridViewRow row in dataGridView1.Rows) 
       // if (dataGridView1.Rows.IndexOf(i%4 ==)) 


       { 
        column0Array[k] = row.Cells[0].Value != null ? row.Cells[0].Value.ToString() : string.Empty; 
        column1Array[k] = row.Cells[1].Value != null ? row.Cells[1].Value.ToString() : string.Empty; 

        k++; 
       } 
     } 

類似這樣的東西?我有未處理的異常...C#:從datagridview中選擇某些行

+0

不要忘記給予好評和accpeted它是否適合您標記的答案... – 2012-04-20 07:31:42

+0

比你需要檢查,如果(行!= NULL),比for循環需要寫下foreach(DataGridViewRow行中的行)比完整的GridView循環...我希望你明白了 – 2012-04-20 07:44:57

+0

我編輯了我的代碼... – 2012-04-20 07:49:30

回答

1
List<DataGridViewRow> rows = null; 
for (int i =0;i<dataGridView1.Rows.Count;i++) 
{ 
    if(i%4==0) 
    rows.Add(dataGridView1.Rows[i]); 

    column0Array[i] = dataGridView1.Rows[i].Cells[0].Value != null ? dataGridView1.Rows[i].Cells[0].Value.ToString() : string.Empty; 
    column1Array[i] = dataGridView1.Rows[i].Cells[1].Value != null ? dataGridView1.Rows[i].Cells[1].Value.ToString() : string.Empty; 

} 
+0

列表 rows = null; //我應該使用什麼組裝參考? – 2012-04-20 07:28:43

+0

@FlorinMihalache - System.Web.UI.WebControls – 2012-04-20 07:30:55

+0

using System.Web.UI.WebControls; ...嗯,我的「項目」沒有看到UI ...我使用VS 2010專業版 – 2012-04-20 07:33:10