How to get database column alias from datatable

  • Thread starter Thread starter mohaaron
  • Start date Start date
M

mohaaron

Does anyone know how I get can to the column alias in a datatable?
here is an example of what I need to do.

Query:
select Column1 as [My Column 1], Column2 as [My Column 2] from
dbo.tablename

Now I need to generate the BoundFields after the query is executed.

foreach (DataColumn column in dataView.Table.Columns)
{
BoundField boundField = new BoundField();
boundField.DataField = column.ColumnName;
boundField.HeaderText = column.Alias;
this.searchGrid.Columns.Add(boundField);
}

The problem is that there is no column.Alias so how can I get the
original column name for the DataField and then the Alias for the
HeaderText?

thanks
 
One approach to this (as I illustrate in my article published several years
ago) is to create an Extended Properties that has this additional metadata.
More recently, I think using a SQLCe database to manage client-side metadata
has some merits. No, we've been asking Microsoft to include additional
metadata in the data stream for a decade (or longer) to little avail.

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
 
What Miha Markic is very true, so bring both info into your datasets and
thenSome dirty way could be as below

Dim da As New Data.SqlClient.SqlDataAdapter("Select Id, Pname as
[Pname-ProductName] from Products", "Data Source=(local)\SQLExpress;Initial
Catalog=TestDB;Integrated Security=True;Pooling=False")
Dim ds As New Data.DataSet()
da.Fill(ds, "P")

[Loop for each fields and split your columnname into two and assign to
appropriate properties.]
Dim arr() As String = ds.Tables("P").Columns(1).ColumnName.Split("-")
ds.Tables("P").Columns(1).ColumnName = arr(0)
ds.Tables("P").Columns(1).Caption = arr(1)

Regards
JIGNESH


Miha Markic said:
Simple: you can't as this piece of information is never transmited.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Does anyone know how I get can to the column alias in a datatable?
here is an example of what I need to do.

Query:
select Column1 as [My Column 1], Column2 as [My Column 2] from
dbo.tablename

Now I need to generate the BoundFields after the query is executed.

foreach (DataColumn column in dataView.Table.Columns)
{
BoundField boundField = new BoundField();
boundField.DataField = column.ColumnName;
boundField.HeaderText = column.Alias;
this.searchGrid.Columns.Add(boundField);
}

The problem is that there is no column.Alias so how can I get the
original column name for the DataField and then the Alias for the
HeaderText?

thanks
 
Back
Top