DataGridView의 특정 열에서만 텍스트를 얻습니다
-
03-07-2019 - |
문제
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;
}
제휴하지 않습니다 StackOverflow