質問

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());
    }
}
役に立ちましたか?

解決

内側の 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());
}

他のヒント

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());
      }
   }
}

おそらくColumnIndexを確認できますか?それでも、すべてのセルをループします。

if (cell.Value == null || cell.Value.Equals("") || cell.ColumnIndex != 2)
{
    continue;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top