簡體   English   中英

使用DataView更新DataGridView數據

[英]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中的舊數據 ),我該怎么做?

我搜索前的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM