firas shaar

firas shaar

  • NA
  • 15
  • 17.8k

how to delete selected row of datagrideview using checkboxes

Jul 31 2014 10:35 AM
my problem when I select 3 selected rows from gridview using checkboxes remove 2 rows and the last row don't remove
what's the solution?
I write this class
public static bool DeletePayments(int id)
{
bool b;

string strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\Salaries.accdb";
OleDbConnection conn = new OleDbConnection(strconn);

if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OleDbCommand cmd = new OleDbCommand("DELETE FROM Payments WHERE Payment_ID =@id;", conn);

cmd.Parameters.Add(new OleDbParameter("@id", id));

int i = cmd.ExecuteNonQuery();

if (i <= 0)
b = false;
else
b = true;
conn.Close();
return b;
}
and at button_click event I write
private void button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridViewPays.Rows.Count; i++)
{
if (GridViewPays.Rows[i].Cells[0].Value != null)
{

if ((bool)GridViewPays.Rows[i].Cells[0].Value == true)
{

GridViewPays.Rows.RemoveAt(i);


if (PaymentsMgr.DeletePayments(int.Parse(GridViewPays.CurrentRow.Cells["Payment_ID"].Value.ToString())))
{
GridViewPays.EndEdit();

}

}

 

Answers (1)