简体   繁体   中英

how to refresh DataGridView after deleting a row form DataGridView and database in c#?

after deleting a selected row of stockdataGridView1, It don't refresh. please tell me how to refresh after deleting selected row. do I need to reopen the form or add another button for refresh ?

private void stockdataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void Report_Load(object sender, EventArgs e)
{
    string connectionString = @"Server=.\SQLEXPRESS; Database = stock; integrated Security = true";
    SqlConnection connection = new SqlConnection(connectionString);
    string query = "SELECT * FROM stocktable1";
    SqlCommand command = new SqlCommand(query, connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    List<Stock> stocks = new List<Stock>();
    while (reader.Read())
    {
        Stock stock = new Stock();
        stock.id = (int)reader["id"];
        stock.gsm = reader["gsm"].ToString();
        stock.color = reader["color"].ToString();
        stock.size = reader["size"].ToString();
        stock.yard = reader["yard"].ToString();
        stock.meter = reader["meter"].ToString();
        stock.quantity = reader["quantity"].ToString();
        stock.supplier = reader["supplier"].ToString();
        stock.purpose = reader["purpose"].ToString();
        stock.chalanno = reader["chalanno"].ToString();
        stocks.Add(stock);
    }
    reader.Close();
    connection.Close();
    stockdataGridView1.DataSource = stocks;
}

private void deletebutton_Click(object sender, EventArgs e)
{

    int id = (int)stockdataGridView1.CurrentRow.Cells["id"].Value;
    string connectionString = @"Server=.\SQLEXPRESS; Database = stock; integrated Security = true";
    SqlConnection connection = new SqlConnection(connectionString);
    string query = "delete from stocktable1 where id=" + id;
    SqlCommand command = new SqlCommand(query, connection);
    connection.Open();
    int rowaffected = command.ExecuteNonQuery();
    connection.Close();
    stockdataGridView1.Update();
    stockdataGridView1.Refresh();  

    MessageBox.Show("deleted");
}

} }

在此处输入图片说明

To refresh the grid after rows are added or deleted you need to set the DataSource property of the grid again and then invoke DataBind() method on the DataGridView object. In your case, you can define the the data source ie your List<Stock> object at the class level and while capturing the click event of the Delete button, rebind the data source as explained previously, to the data grid view.

Add the new method I have created below and adjust the original method as shown below, the method should get called when you load the grid and once you delete it.

As I mentioned within my comments, your code needs adjustment, use functions more odten.

        private void stockdataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void UpdateAndLoad()
        {

            string connectionString = @"Server=.\SQLEXPRESS; Database = stock; integrated Security = true";
            SqlConnection connection = new SqlConnection(connectionString);
            string query = "SELECT * FROM stocktable1";
            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            List<Stock> stocks = new List<Stock>();
            while (reader.Read())
            {
                Stock stock = new Stock();
                stock.id = (int)reader["id"];
                stock.gsm = reader["gsm"].ToString();
                stock.color = reader["color"].ToString();
                stock.size = reader["size"].ToString();
                stock.yard = reader["yard"].ToString();
                stock.meter = reader["meter"].ToString();
                stock.quantity = reader["quantity"].ToString();
                stock.supplier = reader["supplier"].ToString();
                stock.purpose = reader["purpose"].ToString();
                stock.chalanno = reader["chalanno"].ToString();
                stocks.Add(stock);
            }
            reader.Close();
            connection.Close();
            stockdataGridView1.DataSource = stocks;
        }

        private void Report_Load(object sender, EventArgs e)
        {
            UpdateAndLoad();
        }

        private void deletebutton_Click(object sender, EventArgs e)
        {

            int id = (int)stockdataGridView1.CurrentRow.Cells["id"].Value;
            string connectionString = @"Server=.\SQLEXPRESS; Database = stock; integrated Security = true";
            SqlConnection connection = new SqlConnection(connectionString);
            string query = "delete from stocktable1 where id=" + id;
            SqlCommand command = new SqlCommand(query, connection);
            connection.Open();
            int rowaffected = command.ExecuteNonQuery();
            connection.Close();
            stockdataGridView1.Update();
            stockdataGridView1.Refresh();  

            MessageBox.Show("deleted");
            UpdateAndLoad();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM