2017-05-03 22 views
0

データベースから顧客を検索するために使用しているテキストボックスがあります。DataViewを使用してDataGridViewデータを更新する

これは私のコードです:

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: enter image description here

検索後、マイDGV:enter image description here

+0

それはあなたが求めているものは明らかではない - あなたが期待しています'DataSource'を更新するときに更新される' DataGridView'? – Chawin

+0

はい、私はdgvに**このデータが含まれているだけ**とします:https://i.stack.imgur.com/GHW0E.png – Kfir

答えて

0
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; 
+0

私はOleDbではなくSQLを使用しています。 – Kfir

関連する問題