Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datagridview and two buttons. now what i have to do is i have to add new blank row and delete selected row using add and delete button respectively.

plz heplp me out
thanks in advance
Posted
Updated 25-Nov-11 19:05pm
v2
Comments
uspatel 26-Nov-11 1:09am    
In windows application/web ?

Check Out the following Code :

To add New Row into DataGridView :
C#
private void AddButton_Click(object sender, EventArgs e)
    {
      //To Add New Row in DataGridView
      this.dataGridView1.Rows.Add("five", "six", "seven", "eight");
      //To Add New Row in DataGridView at 0 position
      this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");
    }

To add New Row into DataGridView :
C#
private void DeleteButton_Click(object sender, EventArgs e)
    {
     this.dataGridView1.Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
    }

Ihope it will help you.
 
Share this answer
 
Comments
mkcm2011 26-Nov-11 2:40am    
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound..............i am getting this error on addbutton click ......thanks for delete button ...but plz help me out for add also
Manoj K Bhoir 26-Nov-11 10:01am    
I tested my code and it is working fine.This error comes because your DataGridView is bound with DataBase.So if you add values it affects DataBase thats why it will give you error.You have to add row into DataBase then it will automatically display into GridView.
C#
private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
               dataGridViewTrial.Rows.RemoveAt(dataGridViewTrial.CurrentRow.Index);
               sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
               MessageBox.Show(exceptionObj.Message.ToString());
            }
        }


can follow below link to add row in dataridview

click here[^]
Mark as solution if its solved your problem
 
Share this answer
 
Comments
mkcm2011 26-Nov-11 2:40am    
thanks for delete button
Go thru this link,it works

Link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900