none
how to add rows in datagridview programmatically?

    Question

  •  

    hi !

     

        I have bound a table in a datagridview. Now I want to add rows to the datagridview.

    and I want to Edit them. Plz help me How to do this?

     

    Wednesday, June 20, 2007 6:35 AM

Answers

  •  

    Hi Stephen,

     

    You need to set the AllowUserToAddRows to false, Try something like this:

    Code Snippet

    namespace DGV

    {

        public partial class DGVAddNewRowProgrammatically : Form

        {

            public DGVAddNewRowProgrammatically()

            {

                InitializeComponent();

            }

     

            DataTable dt = new DataTable();

            private void DGVAddNewRowProgrammatically_Load(object sender, EventArgs e)

            {

                dt.Columns.Add("a");

                dt.Columns.Add("b");           

                dt.Columns[1].AllowDBNull = false;

                for (int i = 0; i < 10; i++)

                {

                    dt.Rows.Add("a" + i.ToString(), "b" + i.ToString());

                }

     

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.AllowUserToAddRows = false;

     

            }

     

            private void AddBt_Click(object sender, EventArgs e)

            {

                DataRow dr = this.dt.NewRow();

                dr["a"] = "ax";

                dr["b"] = "add item";

                dt.Rows.Add(dr);

            }

        }

    }

     

    Hope this helps.

    Regards.

    Thursday, June 21, 2007 1:26 PM

All replies

  • Add a bindingsource to your form.  Bind the bindingsource to the table.  Bind the datagridview to the bindingsource.  Call the bindingsource's AddNew method to add a row to the datagridview
    Wednesday, June 20, 2007 10:55 AM
  •  

    Hi Stephen,

     

    You need to set the AllowUserToAddRows to false, Try something like this:

    Code Snippet

    namespace DGV

    {

        public partial class DGVAddNewRowProgrammatically : Form

        {

            public DGVAddNewRowProgrammatically()

            {

                InitializeComponent();

            }

     

            DataTable dt = new DataTable();

            private void DGVAddNewRowProgrammatically_Load(object sender, EventArgs e)

            {

                dt.Columns.Add("a");

                dt.Columns.Add("b");           

                dt.Columns[1].AllowDBNull = false;

                for (int i = 0; i < 10; i++)

                {

                    dt.Rows.Add("a" + i.ToString(), "b" + i.ToString());

                }

     

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.AllowUserToAddRows = false;

     

            }

     

            private void AddBt_Click(object sender, EventArgs e)

            {

                DataRow dr = this.dt.NewRow();

                dr["a"] = "ax";

                dr["b"] = "add item";

                dt.Rows.Add(dr);

            }

        }

    }

     

    Hope this helps.

    Regards.

    Thursday, June 21, 2007 1:26 PM
  • Here you can find all about datagridview c# datagridview tutorial

    Bona

    Saturday, January 04, 2014 11:12 AM