Reputation: 637
I am new to C# and this is not duplicated question, I checked similar questions here but not luck.
I have 2 buttons and DataGridView1
.
ButtonAdd
to add a row and buttonDelete
to delete a row.
First, I click Add to add new row and always keep the last row selected.
Second, when I want to delete the last row or any other rows (by clicking on them) it works perfect and the deleted row stays selected (as I wanted ).
Now I add a row then the last row is automatically selected and then when I want to delete the last row (without clicking on it but it is already selected) it gives me an error:
"Index was out of range. Must be non-negative and less than the size of the collection"
But if I click on it, it works and the row stays selected.
So my problem is that I can not delete the last row without clicking on it (but the last row is actually already selected as result of adding row). I want to delete it and keep the row selected. Please help, Thank you
public partial class Form1 : Form
{
Int RowIndex;
Int RowCount;
{
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
RowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.Rows.Add(1, "pizaa", 3); //add rows to dataGridView;
dataGridView1.Rows[RowIndex].Selected = true; //select the added row
}
private void buttonDelete_Click(object sender, EventArgs e)
{
RowCount = dataGridView1.Rows.Count;
RowIndex = RowCount - 1;
switch (RowCount)
{
case 0: /// no Rows to delete
break;
case 1: /// only one Row to delete
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
break;
default: // otherwise
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); //delete the selected
dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Selected = true; // select same row after deleting it.
break;
}
}
}
Upvotes: 1
Views: 2856
Reputation: 454
This accomplishes your intent as I understand it. The private fields RowIndex and RowCount are unnecessary and have been removed. I've applied some non-default property values in the Form constructor so you can see them. Most notably there is a SelectionChanged event handler that determines whether or not the delete button is enabled.
This may not be exactly what you're looking for, but it should keep you going in the right direction.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
buttonDelete.Enabled = false;
dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
dataGridView1.MultiSelect = false;
dataGridView1.AllowUserToAddRows = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
buttonDelete.Enabled = (((DataGridView)sender).SelectedRows.Count > 0);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
int rowIndex = dataGridView1.Rows.Add(1, "pizaa", 3); //add rows to dataGridView;
dataGridView1.Rows[rowIndex].Selected = true; //select the added row
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
var index = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows.RemoveAt(index);
// Select the last row if it exists...
if (dataGridView1.Rows.Count > 0)
{
index = (index == 0) ? 0 : --index;
dataGridView1.CurrentCell.Selected = false;
dataGridView1.Rows[index].Selected = true;
}
}
}
}
Upvotes: 1