我想要引用DataGrid的特定列並將所有值獲取到數組中。我沒有得到任何錯誤,但問題是隻有數組的第零個位置似乎有一個值,而其他位置爲空。Datagrid中有4條記錄。我在做什麼錯查找特定的列,並在Visual C#中獲取數組值#
這裏是我的代碼:
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();
}
}
您正在遍歷行兩次,一次在foreach中,一次在 –
中應該如何更改 –