change a field name in DataGrid

H

HS1

Hello

I have a datagrid to show data for a database table using "seclect * from
tablename"

The datagrid works OK. However, I want to change the name of the fields in
the database to other name in DataGrid. For example, a field name in
database is "FName" will be shown in dataGrid as "First Name". Could you
please tell me how to do that

Thank you for your help
Best regards
S.Hoa
 
D

Doug Bell

Hi
If you use, instead of "SELECT * FROM tblTableA" which returns all fields
using their Field names;
"SELECT fldA AS Orders, fldB AS Suppliers FROM tblTableA Order By fldA"
the data table will use "Orders" as an Alias to the field name fldA and
"Suppliers" as an Alias to the field name fldA. It will also order the data
table by the records in the Field fldA.
 
H

HS1

Hello
I already used Alias, However, if I do that the DataGrid will not display
any data (it shows "null"). I do not know how to set DataGrid to show the
Alias (in design form)
 
D

Doug Bell

Hi,
I am a bit confused.
If you are using Select * then you are not using alias for field names.
With your example it would be:
"SELECT FName AS [First Name] FROM tablename"

the brackets are needed if you use spaces, keywords or special characters.

Doug
 
H

HS1

Thanks
I forget to tell you that I tried to use "Select FName As [FirtName]". When
I did that, in the DataGrid, the column is still "FName" and the value is
"null"
I think that I set wrong DataGrid

Doug Bell said:
Hi,
I am a bit confused.
If you are using Select * then you are not using alias for field names.
With your example it would be:
"SELECT FName AS [First Name] FROM tablename"

the brackets are needed if you use spaces, keywords or special characters.

Doug

HS1 said:
Hello
I already used Alias, However, if I do that the DataGrid will not display
any data (it shows "null"). I do not know how to set DataGrid to show the
Alias (in design form)


fields
 
W

W.G. Ryan eMVP

use a DataGridTableStyle, then apply a DataGridColumnStyle to it. You can
change this by manipulating the DataColumn's name, but I'd recommend against
doing that in most cases b/c you'll need to remember to use that Alias
everywhere and chances are you'll forget at some point (or some other
developer will) .
http://www.knowdotnet.com/articles/cgrid.html
Also, unless you have a really small table and/or actually need every
column, stay away from SELECT * because you'll pull over a lot of data that
you don't need which is cached locally - this can be a real performance
killer. (Doug already mentioned using the aliases instead of SELECT *, I'm
just seconding that opinion)

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
HS1 said:
Thanks
I forget to tell you that I tried to use "Select FName As [FirtName]". When
I did that, in the DataGrid, the column is still "FName" and the value is
"null"
I think that I set wrong DataGrid

Doug Bell said:
Hi,
I am a bit confused.
If you are using Select * then you are not using alias for field names.
With your example it would be:
"SELECT FName AS [First Name] FROM tablename"

the brackets are needed if you use spaces, keywords or special characters.

Doug

HS1 said:
Hello
I already used Alias, However, if I do that the DataGrid will not display
any data (it shows "null"). I do not know how to set DataGrid to show the
Alias (in design form)


Hi
If you use, instead of "SELECT * FROM tblTableA" which returns all fields
using their Field names;
"SELECT fldA AS Orders, fldB AS Suppliers FROM tblTableA Order By fldA"
the data table will use "Orders" as an Alias to the field name fldA and
"Suppliers" as an Alias to the field name fldA. It will also order the
data
table by the records in the Field fldA.

Hello

I have a datagrid to show data for a database table using "seclect *
from
tablename"

The datagrid works OK. However, I want to change the name of the fields
in
the database to other name in DataGrid. For example, a field name in
database is "FName" will be shown in dataGrid as "First Name".
Could
you
please tell me how to do that

Thank you for your help
Best regards
S.Hoa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top