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
{
}

VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 4:59 AM EDT
Is there still an error if you change the code to this:
If so, what is the error?
mike DelvottiRank #7,298 · 244 reputationPosted Mar 19, 2015, 5:37 AM EDT
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");
VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 5:58 AM EDT
mike DelvottiRank #7,298 · 244 reputationPosted Mar 19, 2015, 6:57 AM EDT
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);
}
}
VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 7:23 AM EDT
mike DelvottiRank #7,298 · 244 reputationPosted Mar 19, 2015, 8:44 AM EDT
Thank you Vulpes for sorting the null error issue :-)
VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 8:53 AM EDT
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?
VulpesRank #55 · 29.2K reputationPosted Mar 19, 2015, 7:04 AM EDT
object temp = supplyDataGridView.Rows[i].Cells[1].Value;
be this:
object temp = supplyDataGridView.Rows[i].Cells[0].Value;
mike DelvottiRank #7,298 · 244 reputationPosted Mar 19, 2015, 7:20 AM EDT
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);
}
}
mike DelvottiRank #7,298 · 244 reputationPosted Mar 19, 2015, 7:27 AM EDT
Could it be something to do with the datagrid itself do you think?
mike DelvottiRank #7,298 · 244 reputationPosted Mar 19, 2015, 10:21 AM EDT