2009-12-18 65 views
2

我有通過數據集HOWTO綁定bindingnavigator到datagridview的

public DataSet getDataSet(string query) 
    { 
     DataSet ds = new DataSet(); 
     OleDbDataAdapter da1 = new OleDbDataAdapter(query, sybaseconn); 
     OleDbCommand cmd1 = new OleDbCommand(query, sybaseconn); 
     cmd1.CommandType = CommandType.StoredProcedure; 
     da1.SelectCommand = cmd1; 
     da1.Fill(ds, "tbl"); 
     da1.Dispose(); 
     da1 = null; 
     SybaseconnClose(); 
     return ds; 
    } 

這工作得很好,從數據庫中提取數據的功能。選擇的列是「Nr」和「備註」 我在下一個函數中調用這個函數。 我的datagridview將與數據集綁定。

private void LoadData() 
    { 
    dataGridView1.DataSource = null; 
    Application.DoEvents(); 
    TTT3Dal awdal = new TTT3Dal(); 
    DataSet dsAWIA = awdal.getDataSet("select_tbl"); 
    awdal.dsTTT3 = dsAWIA; 
    dataGridView1.DataSource = dsAWIA.Tables["tbl"].DefaultView; 
    bindingSource1.DataMember = "tbl"; 
    //dataGridView1.DataBindings.Add("Text", bindingSource1, "nr"); 
} 

這工作得很好。 在表單中我調用了最後一個函數和bindingNavigator。

public Form1() 
    { 
    InitializeComponent(); 
    LoadData(); 
    bindingNavigator1.BindingSource = bindingSource1; 
    } 

工作正常,但如果我去掉這條線
//dataGridView1.DataBindings.Add("Text」,bindingSource1, 「NR」); 我收到此錯誤

無法綁定到DataSource上的屬性或列nr。 參數名稱:數據成員

我試圖通過將線

dataGridView1.DataMember = 「TBL」 來解決此;

但後來我得到的錯誤不能創建現場TBL

兒童名單。

可能有人請幫我解決這個,幫我在DataGridView綁定到bindingnavigator

回答

3

沒關係,我的下一個代碼

private void LoadData() 
    { 
    dataGridView1.DataSource = null; 
    TTT3Dal awdal    = new TTT3Dal(); 
    DataSet dsAWIA    = awdal.getDataSet("select_tbl"); 
    awdal.dsTTT3    = dsAWIA; 

    BindingNavigator _bindnav = new BindingNavigator(true); 
    bindingSource1.DataSource = dsAWIA; 
    bindingNavigator1.BindingSource = bindingSource1; 

    dataGridView1.DataSource = bindingSource1; 
    } 
解決它