I have a datagidview where the first column is a checkbox column.
Then a user can check selected rows, click a button and delete those selected rows, this bit works fine.
My problem is, that it throws an error if no check boxes are checked. How can I edit my code below to loop through datagrid first and throw message if no boxes are checked?
My code below:
DialogResult yourAnswer = MessageBox.Show("Are you sure you want to delete the selected items?", "Caption",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (yourAnswer == DialogResult.Yes)
{
for (int i = supplyDataGridView.Rows.Count - 1; i >= 0; i--)
{
object temp = supplyDataGridView.Rows[i].Cells[0].Value;
if (!Convert.IsDBNull(temp) && Convert.ToBoolean(temp))
{
supplyDataGridView.Rows.RemoveAt(i);
}
}
}
else
{
}
mike DelvottiPosted Mar 19, 2015, 2:21 PM
VulpesPosted Mar 19, 2015, 12:53 PM
Was it something to do with the change not been committed when you checked a single row and then clicked the delete button as i was working on that theory when your latest post came in?
mike DelvottiPosted Mar 19, 2015, 12:44 PM
Thank you Vulpes for sorting the null error issue :-)
mike DelvottiPosted Mar 19, 2015, 11:27 AM
Could it be something to do with the datagrid itself do you think?
VulpesPosted Mar 19, 2015, 11:23 AM
mike DelvottiPosted Mar 19, 2015, 11:20 AM
If i click two rows they remove, but if I click one row it doesn't?
for (int i = supplyDataGridView.Rows.Count - 1; i >= 0; i--)
{
object temp = supplyDataGridView.Rows[i].Cells[0].Value;
if (!Convert.IsDBNull(temp) && temp != null && Convert.ToBoolean(temp))
{
supplyDataGridView.Rows.RemoveAt(i);
}
}
VulpesPosted Mar 19, 2015, 11:04 AM
object temp = supplyDataGridView.Rows[i].Cells[1].Value;
be this:
object temp = supplyDataGridView.Rows[i].Cells[0].Value;
mike DelvottiPosted Mar 19, 2015, 10:57 AM
I don't know if you could help on a little strange issue?
For some reason I can only remove more than one row at a time?
Example, If I tick 2 or 3 or 5 rows and click delete they remove fine, however if I only want to remove 1 row and check 1 row it doesn't remove the row? Almost like it only works if theres 2 or more rows checked?
Could it be a problem with the below code snipet?:
for (int i = supplyDataGridView.Rows.Count - 1; i >= 0; i--)
{
object temp = supplyDataGridView.Rows[i].Cells[1].Value;
if (!Convert.IsDBNull(temp) && Convert.ToBoolean(temp))
{
supplyDataGridView.Rows.RemoveAt(i);
}
}
VulpesPosted Mar 19, 2015, 9:58 AM
mike DelvottiPosted Mar 19, 2015, 9:37 AM
Yes your amendment works a treat and doesn't throw error, it must of been the handling the null?
So on another note, how could I show a message if the user clicks delete but hasn't selected any items?
something like: messagebox.show("You must select one or more rows");
VulpesPosted Mar 19, 2015, 8:59 AM
Is there still an error if you change the code to this:
If so, what is the error?