[英]how to refresh DataGridView after deleting a row form DataGridView and database in c#?
删除stockdataGridView1的选定行后,它不会刷新。 请告诉我删除选定行后如何刷新。 我需要重新打开表单或添加另一个刷新按钮吗?
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");
}
} }
要在添加或删除行后刷新网格,您需要再次设置网格的DataSource
属性,然后调用DataGridView
对象上的DataBind()
方法。 在您的情况下,您可以在类级别定义数据源,即您的List<Stock>
对象,并在捕获 Delete 按钮的单击事件时,将数据源重新绑定到数据网格视图,如前所述。
添加我在下面创建的新方法并调整原始方法,如下所示,该方法应该在您加载网格和删除网格时被调用。
正如我在评论中提到的,您的代码需要调整,使用更频繁的功能。
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.