2016-11-15 54 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."); 
    } 
} 
相关问题