简体   繁体   English

c# - 在c#中删除一行表单DataGridView和数据库后如何刷新DataGridView?

[英]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.删除stockdataGridView1的选定行后,它不会刷新。 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.要在添加或删除行后刷新网格,您需要再次设置网格的DataSource属性,然后调用DataGridView对象上的DataBind()方法。 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.在您的情况下,您可以在类级别定义数据源,即您的List<Stock>对象,并在捕获 Delete 按钮的单击事件时,将数据源重新绑定到数据网格视图,如前所述。

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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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