Hello ,
I have created a small application it contains a form with the following controls:
1)2 buttons:
-Add: Adds the data in datagridview.
-Delete: Delete data from the datagridview.
2)DataGridView:
-datagridview1
3)2 textboxes:
- textbox1: enter data into the datagridview through this textbox.
- textbox2: this specifies the index at which you want to remove the data. So enter the index at which you want to remove the data.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("col1", "data"); dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(removed);
}
private void add_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add(textBox1.Text); }
private void delete_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(int.Parse(textBox2.Text)); }
public void removed(object sender, DataGridViewRowsRemovedEventArgs e)
{
MessageBox.Show("Removed "); }
}
- Please remember suppose you have 3 elements in the datagridview. To delete the 3 element you will have to specify the index : 2, because it is a zero based index, starts from 0 not 1. So the exception you might have got is :
Quote:
Uncommitted new row cannot be deleted.
(correct me if i am wrong).
- Just try the above code. Hope it helps. This code adds and deletes an item on button press. Please do let me know what happens.
thanks again,
-Rahul