如何在两个组合框中组合两个表并在Datagridview中显示结果 [英] How Can I Combine Two Tables From Two Comboboxes And Display Result In Datagridview
问题描述
Hi
我有两个组合框,两个组合框都是用sql server中的相同表填充的(例如,T1,T2和T3)。我想动态地从每个组合框中选择一个表格并加入或 合并 所选表格以及按钮和在DataGridView中显示结果。顺便说一句,所有表都有相同的列。
这个小项目是一个c#winforms应用程序。有人可以帮帮我吗
摘要
两个带表格的组合框/>
从每个组合框中动态选择表格
使用Button加入/组合所选值和
最后在Datagridview中显示已连接的表
Hi
I have two comboboxes and both comboboxes are being populated with the same tables from sql server.(eg. T1,T2& T3). I want to dynamically choose a table from each combobox and JOIN or Combine the selected tables together with a BUTTON and display the result in a DataGridView. By the way all Tables have the same columns.
This small project is a c# winforms application. Can somebody please help me
Summary
Two comboboxes with tables
Select table dynamically from each combobox
Join/Combine the the selected values with a Button and
Finally display the joined Tables in Datagridview
推荐答案
尝试使用动态查询它会帮助你
http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql -commands-in-sql-server / [ ^ ]
在存储过程中构建动态SQL [ ^ ]
try to use dynamic query it will help you
http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/[^]
Building Dynamic SQL In a Stored Procedure[^]
您是否期望这样的结果?只是示例我只使用SQL和Temp表来理解这些概念........
Did You Expect Like this Result ? just Example i use only SQL and Temp tables for beter Understanding the concepts ........
Declare @Combobox1 NVARCHAR(30)
Declare @Combobox2 NVARCHAR(30)
SET @Combobox1='@TestTbl1'
SET @Combobox2='@TestTbl2'
Declare @TestTbl1 AS Table (Id Int,Name NVARCHAR(20))
Declare @TestTbl2 AS Table (Id Int,Name NVARCHAR(20))
Declare @TestTbl3 AS Table (Id Int,Name NVARCHAR(20))
INSERT INTO @TestTbl1 (Id,Name) VALUES (1,'A')
INSERT INTO @TestTbl1 (Id,Name) VALUES (2,'B')
INSERT INTO @TestTbl1 (Id,Name) VALUES (3,'C')
INSERT INTO @TestTbl1 (Id,Name) VALUES (4,'D')
INSERT INTO @TestTbl1 (Id,Name) VALUES (5,'E')
INSERT INTO @TestTbl2 (Id,Name) VALUES (6,'F')
INSERT INTO @TestTbl2 (Id,Name) VALUES (7,'G')
INSERT INTO @TestTbl2 (Id,Name) VALUES (8,'H')
INSERT INTO @TestTbl2 (Id,Name) VALUES (9,'I')
INSERT INTO @TestTbl2 (Id,Name) VALUES (10,'J')
INSERT INTO @TestTbl3 (Id,Name) VALUES (11,'K')
INSERT INTO @TestTbl3 (Id,Name) VALUES (12,'L')
INSERT INTO @TestTbl3 (Id,Name) VALUES (13,'M')
INSERT INTO @TestTbl3 (Id,Name) VALUES (14,'N')
INSERT INTO @TestTbl3 (Id,Name) VALUES (15,'O')
IF(@Combobox1='@TestTbl1' AND @Combobox2='@TestTbl2' )
BEGIN
SELECT * FROM @TestTbl1 UNION ALL
SELECT * FROM @TestTbl2
END
ELSE IF(@Combobox1='@TestTbl1' AND @Combobox2='@TestTbl3' )
BEGIN
SELECT * FROM @TestTbl1 UNION ALL
SELECT * FROM @TestTbl3
END
Out Put:
Out Put:
Id Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
这篇关于如何在两个组合框中组合两个表并在Datagridview中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!