public static int[,] DataGridViewToArray(DataGridView dataGridView)
int[,] array = new int[dataGridView.Rows.Count, dataGridView.Columns.Count];
foreach (DataGridViewRow row in dataGridView.Rows)
while (x < dataGridView.Columns.Count)
array[i, x] = (row.Cells[x].Value != null && int.TryParse(row.Cells[x].Value, out value)) ? value : 0;
public static void ArrayToDataGridView(int[,] data, DataGridView dataGridView)
dataGridView.Rows.Clear();
var rowCount = data.GetLength(0);
var rowLength = data.GetLength(1);
for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex)
var row = new DataGridViewRow();
for(int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
row.Cells.Add(new DataGridViewTextBoxCell()
Value = data[rowIndex, columnIndex]
dataGridView.Rows.Add(row);