hello,

I got the below exception while trying to update the datagridview after adding a record into microsoft access database:

c# rows cannot be programmatically added to the datagridview's rows collection when control is data -bound

using below code:

private void btnAddOne_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.Trim();
            string num = textBox2.Text.Trim();

            OleDbConnection conn = null;
            OleDbCommand cmd = null;

            try
            {
                conn = new OleDbConnection(dbconnection);

                conn.Open();

                cmd = new OleDbCommand("INSERT into contact (name, mobile_num, date_created) " + " VALUES (@para1,@para2,@para3)", conn);

                cmd.Parameters.AddWithValue("@para1", name);

                cmd.Parameters.AddWithValue("@para2", num);

                cmd.Parameters.AddWithValue("@para3", DateTime.Now.ToString("d/M/yyyy"));

                cmd.ExecuteNonQuery();

                this.dataGridView1.Rows.Add();


            }
            catch (Exception exp)
            {

                MessageBox.Show(exp.Message);
            }
            finally
            {
                cmd.Dispose();

                conn.Close();
            }
        }

Appreciate if anyone can advice on this. thanks !

regards,
Mark

I'm a bit confused as to your intention here... are you trying to add the item you're inserting into the DB to the already populated DGV? If so, why not simply reload the DGV to reflect the new table contents once the table's been added to?

Hi Lusi,

I inserted a record into mdb and after that I need that record to be displayed in the DGV. However, when I tried it using:

this.dataGridView1.Rows.Add();

I got an exception saying:

c# rows cannot be programmatically added to the datagridview's rows collection when control is data-bound

Can you please further advice?

Thanks in advance!

regards,
Mark

hey i am not sure whether this method will hep you.

get the number of records that u need to display and then write a loop to add that amount of datagrid view rows. and then try to add the data u need to insert into the database...


thankxxxxx

You dont add rows to the datagridview, you add them to the underlying source then refresh the datagridview.
Find the code you used to populate the datagridview and call it again isntead of call Rows.Add()

commented: That's what *I* was saying :) +1
            System.Data.SqlClient.SqlConnection Cn = new System.Data.SqlClient.SqlConnection();
            Cn.ConnectionString = "server=(local);database=Mirak Kish;trusted_connection=true";
            System.Data.SqlClient.SqlCommand Cmd = new System.Data.SqlClient.SqlCommand();
            Cmd.Connection = Cn;
            Cmd.CommandType = CommandType.Text;
            Cmd.CommandText = "select * from Moshtari where MID>=@m1 and MID<=@m2 ";
            Cmd.Parameters.AddWithValue("@m1", textBox1.Text);
            Cmd.Parameters.AddWithValue("@m2",textBox2.Text);

            try
            {
                Cn.Open();
                SqlDataReader Rdr = Cmd.ExecuteReader();
                int i = 0;
                while (Rdr.Read()) 
                {

                        dataGridView1.Rows[i].Cells[0].Value = Rdr["MID"].ToString();
                        dataGridView1.Rows[i].Cells[1].Value = Rdr["Name"].ToString();

                        i = i + 1;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            finally
            {
                Cn.Close();
            }

Hello. if you can't add rows to bound datagridview, so i have one idea.
First: create local variable Datatable table;

Example:

             string strSQL = "..your code..";
             string strcon="..your connection string..";
            SqlDataAdapter da = new SqlDataAdapter(strSQL,strcon);
            SqlCommandBuilder cmdbldr = new SqlCommandBuilder(da);
            table = new DataTable();
            da.Fill(table);

            BindingSource bs = new BindingSource();
            bs.DataSource = table;
            dataGridView1.DataSource = bs;
this datagridview filled by this table;

private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {


                table.Rows.Add();


            }
            catch(Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

So you can add row to datagridview in runtime.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.