2009-05-03 4 views
3

DataGridViewには4列と複数行のデータが格納されています。私はDataGridViewを反復処理し、から特定の列のみのセル値を取得したいと思います。このデータをメソッドに渡す必要があるためです。ここでDataGridViewの特定の列からのテキストの取得

が私のコードです:

foreach (DataGridViewRow row in this.dataGridView2.Rows) 
{        
    foreach (DataGridViewCell cell in row.Cells) 
    { 
     if (cell.Value == null || cell.Value.Equals("")) 
     { 
      continue; 
     } 

     GetQuestions(cell.Value.ToString()); 
    } 
} 

これはちょうどしかし、私のようなものを指定できるようにする必要があり、すべてのセルを通過するようだ:

foreach (DataGridViewRow row in this.dataGridView2.Rows) 
{        
    foreach (DataGridViewCell cell in row.Cells[2])//Note specified column index 
    { 
     if (cell.Value == null || cell.Value.Equals("")) 
     { 
      continue; 
     } 
     GetQuestions(cell.Value.ToString()); 
    } 
} 

答えて

5

あなただけの必要はありません。内側を取り除くにはforeachループ?または私は何かを逃したか?

foreach (DataGridViewRow row in this.dataGridView2.Rows) 
{        
    DataGridViewCell cell = row.Cells[2]; //Note specified column index 
    if (cell.Value == null || cell.Value.Equals("")) 
    { 
     continue; 
    } 

    GetQuestions(cell.Value.ToString()); 
} 
+0

ありがとう、長すぎるのコーディングアップして、それはそう...... :-D – Goober

+0

それは私の時間帯で朝の真ん中ですので、あなたはに恩返しをする必要がある場合があります12時間の時間;) –

+0

haha​​、私はあなたがあまりにもあると推測するところのイギリスにいます..... – Goober

2

おそらく、あなたはColumnIndexをチェックできますか?しかし、まだすべての細胞をループします。

if (cell.Value == null || cell.Value.Equals("") || cell.ColumnIndex != 2) 
{ 
    continue; 
} 
3
foreach (DataGridViewRow row in this.dataGridView2.Rows) 
{ 
    DataGridViewCell cell = row.Cells["foo"];//Note specified column NAME 
    { 
     if (cell != null && (cell.Value != null || !cell.Value.Equals(""))) 
     { 
     GetQuestions(cell.Value.ToString()); 
     } 
    } 
} 
+0

ありがとうございます...私は同じ文脈ではありませんが、このコードを探していました... –

関連する問題