C# Exercises

Home AgriMetSoft About Contact

Array to DataGridView in C# and Programmatically insert DataGridView

	
    private void button1_Click(object sender, EventArgs e)
    {
      double[,] array = new double[,] { { 2, 4, 6 }, { 3.2, 2.4, 8.3 } };
      DataGridView dataGridView1 = new DataGridView();
      dataGridView1.Dock = DockStyle.Fill;
      splitContainer1.Panel2.Controls.Add(dataGridView1);
      dataGridView1.DataSource = array2DataTable<double>(array);
    }

    private DataTable array2DataTable<T>(T[,] arr)
    {
      DataTable dt = new DataTable();
      for (int i = 0; i < arr.GetLength(0); i++)
      {
        dt.Columns.Add();
      }

      for (int j = 0; j < arr.GetLength(1); j++)
      {
        // create a DataRow using .NewRow()
        DataRow row = dt.NewRow();
        // iterate over all columns to fill the row
        for (int i = 0; i < arr.GetLength(0); i++)
        {
          row[i] = arr[i, j];
        }
        // add the current row to the DataTable
        dt.Rows.Add(row);
      }
      return dt;
    }
	


Download the project of Visual Studio 2013 in DropBox Download


Array to DataGridView || Insert DataGridView Programmatically


List of Exercises