[英]Update DataGridView Data Using DataView
我有一個文本框,用於從數據庫中搜索客戶。
這是我的代碼:
DataView dv = new DataView(tabSearchCustomer);
dv.RowFilter = string.Format("CONVERT({0},System.String) LIKE '%{1}%'", "ID", txtboxSearchCustomer.Text);
dgvCustomers.DataSource = dv;
dgvCustomers.DataSource = dv僅顯示新數據,而不替換dgv中的數據。
我想用搜索到的新數據更新dgv( 替換dgv中的舊數據 ),我該怎么做?
public static DataTable ExecuteQuery(string storedProcedure, SqlParameter[] parameters)
{
using (SqlConnection sqlConnection = new SqlConnection(DLLConfig.GetDataBaseConnection()))
{
DataTable dataTable = new DataTable();
SqlCommand sqlCommand = new SqlCommand(storedProcedure, sqlConnection);
sqlCommand.CommandTimeout = 0;
sqlCommand.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in parameters)
{
sqlCommand.Parameters.Add(param);
}
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dataTable);
SqlConnection.ClearAllPools();
return dataTable;
}
}
用法:
dataGridView1.DataSource = ExecuteQuery().DefaultView;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.