2016-11-15 98 views
0

我有SQL列表,其中有三列ID,CUSTOMERNAMEADDRESS使用組合框將數據從sql數據庫加載到datagridview中

我想在combobox1中顯示所有客戶ID的列表,當我選擇任何一個ID時,客戶名稱和地址應顯示在datagridview1屬於該ID的位置。

請諮詢我的代碼爲我學習編程的C#

下面是我的app.config供你參考

<connectionStrings> 
    <add name="dbx" 
     connectionString="Data Source=USER\SQLEXPRESS;Initial Catalog=dbInventory;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 
+0

請分享您嘗試使用的代碼。 –

+0

我如何分享代碼請指導我。我無法在此寫入 –

+0

您可以編輯您的帖子併爲您提到的問題添加相關的aspx和cs代碼。您還可以使用適當的編輯器功能來格式化文本。看到屏幕截圖.. http://prntscr.com/d8b0c1 –

回答

1

這是簡單的,你可以給一個嘗試。

private void BindData(string selectCommand) 
{ 
    try 
    { 
     string selectCommand = "SELECT * FROM tblCustomers"; 
     String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;; 

     // Create a new data adapter based on the specified query. 
     var dataAdapter = new SqlDataAdapter(selectCommand, connectionString); 

     // Create a command builder to generate SQL update, insert, and 
     // delete commands based on selectCommand. These are used to 
     // update the database. 
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); 

     // Populate a new data table and bind it to the BindingSource. 
     DataTable table = new DataTable(); 
     dataAdapter.Fill(table); 
     dataGridView1.DataSource = table; 
    } 
    catch (SqlException) 
    { 
     MessageBox.Show("To run this example, replace the value of the " + 
      "connectionString variable with a connection string that is " + 
      "valid for your system."); 
    } 
} 
相關問題