【问题标题】:Update DataGridView Data Using DataView使用 DataView 更新 DataGridView 数据
【发布时间】:2017-05-03 17:44:16
【问题描述】:

我有一个文本框,用于从我的数据库中搜索客户。

这是我的代码:

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:

【问题讨论】:

  • 不清楚您在问什么 - 您是否希望在更新 DataSource 时更新 DataGridView
  • 是的,我希望我的 dgv 包含这些数据:i.stack.imgur.com/GHW0E.png

标签: c# winforms datagridview


【解决方案1】:
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;

【讨论】:

  • 我使用的是 OleDb 而不是 SQL。
猜你喜欢
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多