2011-07-25 6 views
0

データベースからデータセットmyDSにデータをロードしました。 myDSには複数のテーブルが含まれています。次に、これらのテーブルの1つ(インベントリ)をという名前のデータグリッドビューにバインドしました。データグリッドビューのが正常に動作しました。 しかし、インベントリテーブルで検索しようとしたときに、結果がdatagridview dataGridViewInventoryにバインドされましたが、空に見えました。ここでデータセットC#から検索するときにデータセットにデータをバインドできません。

は、検索およびバインディングのコードです:

DataRow[] filteredRows = 
         myDS.Tables["Inventory"].Select(string.Format("Make LIKE '%{0}%'", txtMake.Text.Trim())); 

      DataSet tempTaskDS = new DataSet("tempCars"); 

      //tempTaskDS.Tables.Add("TempInv"); 

      DataTable DataTable2 = new DataTable("TempInv"); 
      // This makes the new DataTable have the same columns as the existing DataTable. 
      DataTable2 = myDS.Tables["Inventory"].Clone(); 

      foreach (DataRow r in filteredRows) 
      { 
       DataTable2.ImportRow(r); 
      } 
      tempTaskDS.Tables.Add(DataTable2); 

      dataGridViewInventory.DataSource = tempTaskDS.Tables["TempInv"]; 

誰もが、私が間違っているものを私にしてください教えてください。

答えて

0

Foundソリューション: 一時データセットを使用する必要はありません.BindingSourceを使用してデータテーブルをバインドしてから、BindingSourceをDataGridviewにバインドしてください。コードは次のとおりです。

DataRow[] filteredRows = 
         myDS.Tables["Inventory"].Select(string.Format("Make LIKE '%{0}%'", txtMake.Text.Trim())); 

      DataTable DataTable2 = new DataTable("TempInv"); 
      // This makes the new DataTable have the same columns as the existing DataTable. 
      DataTable2 = myDS.Tables["Inventory"].Clone(); 

      foreach (DataRow r in filteredRows) 
      { 
       DataTable2.ImportRow(r); 
      } 

      BindingSource bSource = new BindingSource(); 
      bSource.DataSource = DataTable2; 

      dataGridViewInventory.DataSource = bSource; 
関連する問題