DataGridの特定の列を参照し、すべての値を配列に取得しようとしています。エラーは発生しませんが、問題は、配列の0番目の位置だけが値を持っているように見え、他の位置はnullであるということです。データグリッドには4つのレコードがあります。私は何を間違っているのですか
これが私のコードです:
private void button1_Click(object sender, EventArgs e)
{
string[] arr = new string[10];
DataTable getdata = new DataTable();
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
DataGridViewCell cell = row.Cells[1];
{
if (cell != null && (cell.Value != null))
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
arr[i] = cell.Value.ToString();
}
}
}
}
if (arr[0] != null)
{
textBox3.Text = arr[0].ToString();//Prints value
}
else if (arr[1] != null)//Seems to be null
{
textBox2.Text = arr[1].ToString();
}
}