Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Adding Rows With a 2D Array
 

Adding Rows With a 2D Array

So My Array Looks like this:
(0,0) = "Column One"
(0,1) = " Item One"
(0,2) = " Item Two"
(1,0) = "Column Two"
(0,1) = " Item One"
(0,2) = " Item Two"
(2,0) = "Column Three"
(0,1) = " Item One"
(0,2) = " Item Two"

How can I add this to a DataGrid?
Actual Code Please?

Biodegradable  Saturday, February 03, 2007 3:37 AM

I prefer convert it to datatable first like follow demo,

hope it help you

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        private void Form2_Load(object sender, EventArgs e)

        {

            string [][] array=new string[3][];

            array[0] = new string[] {"Column One", " Item One", " Item Two" };

            array[1] = new string[] {"Column Two", " Item One", " Item Two","Item three"};

            array[2] = new string[] {"Column Three", " Item One", " Item Two"};

            DataTable TB = new DataTable();

            for (int i = 0; i < array.Length; i++)

            {

                TB.Columns.Add(array[ i ][0].ToString(), typeof(string));

            }

            for (int i = 1; i < array[0].Length; i++)

            {

                TB.Rows.Add(array[0][ i ].ToString(), array[1][ i ].ToString(), array[2][ i ].ToString());

            }

            this.dataGridView1.DataSource = TB;   

        }

    }

}

Bob zhu - SJTU  Sunday, February 04, 2007 10:56 AM

I prefer convert it to datatable first like follow demo,

hope it help you

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        private void Form2_Load(object sender, EventArgs e)

        {

            string [][] array=new string[3][];

            array[0] = new string[] {"Column One", " Item One", " Item Two" };

            array[1] = new string[] {"Column Two", " Item One", " Item Two","Item three"};

            array[2] = new string[] {"Column Three", " Item One", " Item Two"};

            DataTable TB = new DataTable();

            for (int i = 0; i < array.Length; i++)

            {

                TB.Columns.Add(array[ i ][0].ToString(), typeof(string));

            }

            for (int i = 1; i < array[0].Length; i++)

            {

                TB.Rows.Add(array[0][ i ].ToString(), array[1][ i ].ToString(), array[2][ i ].ToString());

            }

            this.dataGridView1.DataSource = TB;   

        }

    }

}

Bob zhu - SJTU  Sunday, February 04, 2007 10:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Designer Error when setting BindingSource.Datasource = SomeOtherBindingSource
• Help me to disable allow addnew in my dataGrid
• How to add Checkbox in DataGridView column Header
• accessing datagridview
• How to find a certain record in a DataTable?
• DataGridViewComboBox binding isues
• Calculated Databind in a DateTimePicker
• datagrid in VS 2003
• problem at datagridview
• How to disable deault sorting in datagridview without extending the datagrid?