Search code examples
c#ms-accessdatagridviewoledb

Delete selected rows from DataGridView after password is correct


I want to delete selected rows from a datagridview that is connected to an Access Database after the user enters correct password in another form.

I created a method that should be called from form 2 after password is correct, but nothing happens.

Form1:

public void DeleteSelectedRows()
        {
            foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
            {
                string ID = item.Cells[0].Value.ToString();
                conn_SN.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = conn_SN;
                command.CommandText = "DELETE * FROM SN WHERE ID=" + ID;
                command.ExecuteNonQuery();
                conn_SN.Close();
            }
        }

This is how I call the method from form 2:

private void btnOK_Click(object sender, EventArgs e)
{
    if(textbox.Text=="admin")
    {
        Form1 form = Form1();
        form.DeleteSelectedRows();
    }
}

Any ideas?


Solution

  • I've managed to call the method like this:

    private void btnOK_Click(object sender, EventArgs e)
    {
        if(textbox.Text=="admin")
        {
        var form = Application.OpenForms.OfType<Meniu>().Single();
        form.DeleteSelectedRows();
        }
    }