2016-02-10 18 views
0

我有7 comboboxes在我的項目中我使用comboboxs從數據庫中選擇的數據。第一combobox是工作,但其他人表現出這樣的:System.Data.DataRowView?

System.Data.DataRowView

我能做些什麼來解決這個錯誤這是comboboxes代碼?

private void client_Load(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(); 
     con.ConnectionString = "Data Source=.;Initial Catalog=SCP_DB;Integrated Security=True"; 
     con.Open(); 
     string query = "select Operation_Type from Operations_Types; select Payment_Types from Payment_Type; select Property_Types from Property_Type; select City from Flats; select Section from Flats; select Block from Flats; select Street from Flats"; 
     SqlDataAdapter da = new SqlDataAdapter(query, con); 
     da.TableMappings.Add("Table", "Operations_Types"); 
     da.TableMappings.Add("Table1", "Payment_Type"); 
     da.TableMappings.Add("Table2", "Property_Type"); 
     da.TableMappings.Add("Table3", "Flats"); 
     da.TableMappings.Add("Table4", "Flats"); 
     da.TableMappings.Add("Table5", "Flats"); 
     da.TableMappings.Add("Table6", "Flats"); 
     DataSet ds = new DataSet(); 

     da.Fill(ds, "Operations_Types"); 
     comboOpType.DisplayMember = "Operation_Type"; 
     comboOpType.ValueMember = "Operation_Type"; 
     comboOpType.DataSource = ds.Tables["Operations_Types"]; 

     da.Fill(ds, "Payment_Type"); 
     comboPayType.DisplayMember = "Payment_Types"; 
     comboPayType.ValueMember = "Payment_Types"; 
     comboPayType.DataSource = ds.Tables["Payment_Type"]; 

     da.Fill(ds, "Property_Type"); 
     comboProType.DisplayMember = "Property_Types"; 
     comboProType.ValueMember = "Property_Types"; 
     comboProType.DataSource = ds.Tables["Property_Type"]; 

     da.Fill(ds, "Flats"); 
     comboCity.DisplayMember = "City"; 
     comboCity.ValueMember = "City"; 
     comboCity.DataSource = ds.Tables["Flats"]; 

     da.Fill(ds, "Flats"); 
     comboSection.DisplayMember = "Section"; 
     comboSection.ValueMember = "Section"; 
     comboSection.DataSource = ds.Tables["Flats"]; 

     da.Fill(ds, "Flats"); 
     comboBlock.DisplayMember = "Block"; 
     comboBlock.ValueMember = "Block"; 
     comboBlock.DataSource = ds.Tables["Flats"]; 

     da.Fill(ds, "Flats"); 
     comboStreet.DisplayMember = "Street"; 
     comboStreet.ValueMember = "Street"; 
     comboStreet.DataSource = ds.Tables["Flats"]; 

     SqlCommand cmd = new SqlCommand(query, con); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 


    } 
+1

請添加更多的細節。我不明白你的問題 –

+0

你如何期望程序區分每個不同的「單位」?這是你在這裏遇到的許多問題之一。 – Takarii

回答

2

你應該充滿DataSet只有一次,SqlDataAdapter處理,它包含多個結果集的情況。你可以事後給他們的名字:

SqlDataAdapter da = new SqlDataAdapter(query, con); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
ds.Tables[0].TableName = "Operations_Types"; 
ds.Tables[1].TableName = "Property_Type"; 
// ... 
comboOpType.DisplayMember = "Operation_Type"; 
comboOpType.ValueMember = "Operation_Type"; 
comboOpType.DataSource = ds.Tables["Operations_Types"]; 
comboPayType.DisplayMember = "Payment_Types"; 
comboPayType.ValueMember = "Payment_Types"; 
comboPayType.DataSource = ds.Tables["Payment_Type"]; 
// ... 

否則Fill將追加在每個連續通話的所有記錄和表。

相關問題