Deletimg row from datagridview

Discussion in 'C#' started by kermit2012, Jan 30, 2012.

  1. kermit2012

    kermit2012 New Member

    Joined:
    Jan 29, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have got a delete button on my DataGridView and when I press it, I want it to delete the row selected, I have currently got the code below, but when I press it nothing happens, can anybody tell me why?

    thanks

    Code:
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    string queryString = "SELECT AccountID, Title, FirstName, LastName, AccountType, Username, UserPass FROM AddAccount";
    int currentRow = int.Parse(e.RowIndex.ToString());
    
    {
    string movieIDString = dataGridView1[0, currentRow].Value.ToString();
    movieIDInt = int.Parse(movieIDString);
    }
    if (dataGridView1.Columns[e.ColumnIndex] == deleteButton && currentRow >= 0)
    {
    //delete sql query
    string queryDeleteString = "DELETE FROM Account where AccountID = '"+movieIDInt+"'";
    OleDbCommand sqlDelete = new OleDbCommand();
    sqlDelete.CommandText = queryDeleteString;
    sqlDelete.Connection = database;
    sqlDelete.ExecuteNonQuery();
    loadDataGrid(queryString);
    }
    }
    }
    }
    
     
  2. Ami Desai

    Ami Desai Member

    Joined:
    Jan 5, 2017
    Messages:
    42
    Likes Received:
    17
    Trophy Points:
    8
    Location:
    Ahmedabad
    Home Page:
    http://www.ifourtechnolab.com/
    Hi,

    You can check this

    Code:
    private void btnDelete_Click(object sender, EventArgs e)
    {
         foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
         {
             dataGridView1.Rows.RemoveAt(item.Index);
         }
    }

    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice