2010-12-02 52 views
0

我試圖從窗體單擊查看按鈕後,從數據庫檢索數據,但每次我點擊查看按鈕時,DataGridView控件中的多個行復制數據庫中的相同數據,而不是檢索相同的數據每次點擊查看按鈕時從DataGridView中開始的數據。DataGridView數據檢索混淆

 //For view button 
     private void button2_Click(object sender, EventArgs e) 
     { 
       BindingSource bindingSource = new BindingSource(); 
       bindingSource.DataSource = businesslayer.View("Select * from itemmaster"); //passed to business access layer class 
       dataGridView1.DataSource = bindingSource; 

     } 


//Method in DataAccessLayer class 
public DataTable View(String query) 
     { 
      //Initialize a connection object 
      OpenConn(); 

      //Initalize a command object with passing string value 
      command = new SqlCommand(query, connection); 
      SqlDataAdapter da = new SqlDataAdapter(); 
      da.SelectCommand = command; 

      //Fill dataset with a table 
      da.Fill(dataset, thisTable); 
      return dataset.Tables[thisTable]; 
     } 

誰能告訴我怎樣才能解決這個問題

回答

0
BindingSource bindingSource = new BindingSource(); 
dataGridView1.Items.Clear(); 
bindingSource.DataSource = businesslayer.View("Select * from itemmaster"); //passed to business access layer class 
dataGridView1.DataSource = bindingSource; 

嘗試將它綁定到你的數據源之前清除你的GridView。

+0

但是,我還沒有在DataGridView類中找到任何Items屬性,那麼它如何可以是真的? – Jackson 2010-12-02 10:46:31