troubleshooting Question

Windows form databound datagridview:Add row functionality

Avatar of rishaan
rishaan asked on
1 Comment1 Solution972 ViewsLast Modified:
I have set the AllowUsersToAddRow property to true which rows at the bottom all the time.
I want to control this with the click of the button. On clicking of a button I want to add a new row to datagridview. and allow editing until saved.

My grid is noneditable for oldrows and editable for newrows........I have set  
grid.readonly false,col.readonly fale.,grid.rowtemplate.readonly true......This makes my grid noneditable...
Now with this scenario
If I use AllowUserToAddRows=true
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
            if (this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].IsNewRow)
                this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].ReadOnly = false;

I get editable row which is not added as a result of a click of button/....
If I try to set AllowUserToAddRows =false and then try on click of button
private void button1_Click(object sender, EventArgs e)
        //    cm.EndCurrentEdit();
        //    cm.AddNew();
            DataTable dt = ((DataSet)((BindingSource)this.dataGridView1.DataSource).DataSource).Tables[0];
            DataRow dr = dt.NewRow();
            dr["CustomerID"] = "jyo";
            dr[1] = "item";
            this.dataGridView1.VirtualMode = true;
            //this.dataGridView1.RowCount = dt.Rows.Count + 1;
            this.dataGridView1.AllowUserToAddRows = false;
This adds a row which is controlled by buttonclick but is noneditable

I want a controlled row added on click of button which is editable though old records in grid is noneditable.

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros