开发者

SQL query column names in datagridview

开发者 https://www.devze.com 2023-01-12 05:07 出处:网络
I have two tables test1 { id, name } and test2 { id, name, family } and I write this query: SELECT dbo.test1.*, dbo.test2.*

I have two tables test1 { id, name } and test2 { id, name, family } and I write this query:

SELECT dbo.test1.*, dbo.test2.*
FROM dbo.test1
CROSS JOIN dbo.test2

but i开发者_如何学Cn datagridview1, I want to show the fields (on header) like this:

test1.id test1.name test2.id test2.name test2.family,

but instead they are displayed like this

id name id name family

which changes are needed for my query.


The short answer is: you can change the caption of the grid columns as you like. You can also reorder/hide/sort columns as you like.


I would write the select like:

select t1.id as "test1.Id", 
       t1.name as "test1.Name", 
       t2.id as "test2.Id", 
       t2.name as "test2.Name", 
       t2.family as "Test2.Family" 
from test1 t1, test2 t2

But with that query you will get a cartesian product, if you don't add a proper Where Clause.


You'll need to select the columns individually, and use the as keyword:

SELECT dbo.test1.id as test1id, dbo.test2.id as test2id ...
FROM dbo.test1
CROSS JOIN dbo.test2


Are you asking how to alias the columns?

SELECT
    t1.id AS [test1.id],
    t1.name AS [test1.name] ,
    t2.id AS [test2.id], 
    t2.name AS [test2.name] ,
    t2.family AS [test2.family]
FROM dbo.test1 t1
CROSS JOIN dbo.test2 t2

As the names you want don't meet the standard rules for identifiers they need to be quoted.


Bad code is bad. However, I would do it as @Scoregraphic mentioned. Even if all of your columns come back as co1; col1; col1, you can change the order and labeling. Use the properties for the columns in the DataGridView.

0

精彩评论

暂无评论...
验证码 换一张
取 消