2个数据库表,1个组合框选择字段,用所选数据填充datagridview

编程入门 行业动态 更新时间:2024-10-23 13:25:57
本文介绍了2个数据库表,1个组合框选择字段,用所选数据填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我正在开发一个简单的小程序,只是为了更好地完成一些事情并更好地理解一些代码。它涉及c#和SQL。 我有2个数据库表,它们有一个ORDERID的公共字段。我的C#程序中有2个控件,一个组合框和一个数据网格视图。我有一个组合框填充了其中一个表的所有orderid,我希望我的程序执行此操作。 当我在组合框中选择一个有序时,我想用所有的数据库填充datagridview另一张桌子的特定顺序。 这就是我现在所拥有的,我想知道我是否可以从这个简单的小任务中得到帮助。

// 这会填充组合框,其中包含1个表订单ID idk,如果这是正确的方式 public partial 类 MainWindow:窗口 { public static string connectionString = @ 数据源=(LocalDB)\ V11.0; AttachDbFilename = | DataDirectory目录| \Centu rionDB.mdf; Integrated Security = True; public MainWindow() { InitializeComponent(); 使用(SqlConnection sqlcon = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand( SELECT * FROM [dbo]。[Orders],sqlcon); sqlcon.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ddlOrders.Items.Add(reader [ OrderID]。ToString()); } reader.Close(); } } // 我的数据库数据 INSERT INTO [dbo]。[Orders](OrderID,Type,Price)VALUES( 1111 ,' gG', 1 。 1 ) INSERT INTO [dbo]。[Orders](OrderID,Type,Price)VALUES( 2222 ,' BP', 1 。 2 ) INSERT INTO [dbo]。[Orders](OrderID,Type,Price)VALUES( 3333 ,' gG', 1 。 3 ) INSERT INTO [dbo]。[buy](buysID,OrdersID,item,Type)VALUES( 121 , 1111 , ' 监控',' gG') INSERT INTO [dbo]。[buys](buysID,OrdersID,item,Type)VALUES( 122 , 1111 ,' 键盘',' gG') INSERT INTO [dbo]。[buys](buysID, OrdersID,item,Type)VALUES( 131 , 2222 , ' TV',' BP') INSERT INTO [dbo]。[buys](buysID,OrdersID,item,Type)VALUES( 132 , 2222 ,' Speakers',' BP') INSERT INTO [dbo]。[buys](buysID,OrdersID,item,Type)VALUES( 141 , 3333 ,' sk8', ' sk8') INSERT INTO [dbo]。[buys](buysID,OrdersID, item,Type)VALUES( 142 , 3333 ,' bearing',' sk8')

解决方案

在组合框的点击事件中试用此代码

尝试 { SqlCommand cmd1 = new SqlCommand( 从购买中选择*,其中OrdersID = + ddlOrders.SelectedValue + ,sqlcon); sqlcon.Open(); SqlDataReader readBuys = cmd1.ExecuteReader(); while (readBuys.Read()) { datagridview1.Clear(); datagridview1.DataSource = readbuys; } } catch (Exception exp) { }

Hello, I hve a small simple program I am working on just to get better at a few things and understand a little bit of code better. Its involving c# and SQL. I have 2 database tables and they have a common field which is ORDERID. I have 2 controls in my C# program a combo box and a datagrid view. I have the combo box populated with all the orderids of one of the tables and I want my program to do this. When I select a ordered in the combo box I want to populate the datagridview with all the specific orderids of the other table. This is what I currently have and I was wondering if I can get help with this small simple task.

//this populates the combo box with 1 tables order ids idk if this is the right way tho public partial class MainWindow : Window { public static string connectionString= @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CenturionDB.mdf;Integrated Security=True"; public MainWindow() { InitializeComponent(); using (SqlConnection sqlcon = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[Orders]", sqlcon); sqlcon.Open(); SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { ddlOrders.Items.Add(reader["OrderID"].ToString()); } reader.Close(); } } //my database data INSERT INTO [dbo].[Orders](OrderID, Type, Price) VALUES(1111, 'gG', 1.1) INSERT INTO [dbo].[Orders](OrderID, Type, Price) VALUES(2222, 'BP', 1.2) INSERT INTO [dbo].[Orders](OrderID, Type, Price) VALUES(3333, 'gG', 1.3) INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(121, 1111, 'Monitor', 'gG') INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(122, 1111, 'Keyboard', 'gG') INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(131, 2222, 'TV', 'BP') INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(132, 2222, 'Speakers', 'BP') INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(141, 3333, 'sk8', 'sk8') INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(142, 3333, 'bearings', 'sk8')

解决方案

Try this code at the click event of combobox

try{ SqlCommand cmd1=new SqlCommand("Select * from buys where OrdersID="+ddlOrders.SelectedValue+"",sqlcon); sqlcon.Open(); SqlDataReader readBuys = cmd1.ExecuteReader(); while(readBuys.Read()) { datagridview1.Clear(); datagridview1.DataSource=readbuys; } } catch(Exception exp) { }

更多推荐

2个数据库表,1个组合框选择字段,用所选数据填充datagridview

本文发布于:2023-08-05 21:31:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   字段   所选   数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!