2016-11-15 6 views
0

私は3つのカラムID,CUSTOMERNAMEおよびADDRESSを持つSQLテーブルを持っています。コンボボックスの値を使用してSQLデータベースからdatagridviewにデータをロード

私はすべての顧客IDのリストをcombobox1に表示したいのですが、IDを1つ選択すると、顧客名と住所がdatagridview1に表示されます。私はプログラミングを勉強として

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."); 
    } 
} 
関連する問題