467,915 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Expression Columns problem when using ObjectDataSource

Hi,

I’m having some troubles trying to bind my dataset to a GridView control
through an ObjectDataSource control. The binding works fine for regular
columns in my dataset, but I can’t seem to get my expression columns to show
up in my GridView. Anybody knows any neat tricks to make this work?

Here’s a “step-by-step” to reproduce my worries:

1. Create a new “ASP.NET web site” project
2. Add the “ASP.NET folder” called “App_Code” and add a dataset to this folder
3. Set up the datatable adapter to select some columns from e.g. the
Northwind Customers table, for instance the “CustomerID”, “ContactName”, and
“ContactTitle” columns.
4. Add a column to the dataset table and call it “ContactTitleName”. Set the
“Expression” property to “ContactTitle + ' ' + ContactName”.
5. Drag an ObjectDataSource control on to a web page
6. Set up the datasource to get data from your newly created dataset
tableadapter
7. Add a GridView control and set it’s datasource to your newly created
ObjectDataSource
8. Run the project
9. All columns except the expression column called “ContactTitleName” are
rendered

--
Sincerely,

Kjetil Klaussen
Application Developer, Trondheim
Dec 1 '05 #1
  • viewed: 2010
Share:
2 Replies
Hi Kjetil,

The ObjectDataSource, in your code below, was using the DataAdapter (which
did not have a definition for the expression column). You needed to fill the
DataTable then bind it to the GridView in order for the expression column to
be used (in the manner you did it), e.g. you needed to add code similar to
this in the CodeFile:

DataSet ds = new DataSet();
DataSetTableAdapters.CustomersTableAdapter adapter1 = new
DataSetTableAdapters.CustomersTableAdapter ();
adapter1.Fill(ds.Customers);
GridView1.DataSource = ds.Customers ;
GridView1.DataBind();

Otherwise, the declarative syntax would have worked, had you defined the new
column in the DataAdapter by modifying the Query:
SELECT CustomerID, ContactName, ContactTitle, ContactTitle+'
'+ContactName as ContactTitleName
FROM Customers

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Kjetil Klaussen" wrote:
Hi,

I’m having some troubles trying to bind my dataset to a GridView control
through an ObjectDataSource control. The binding works fine for regular
columns in my dataset, but I can’t seem to get my expression columns to show
up in my GridView. Anybody knows any neat tricks to make this work?

Here’s a “step-by-step” to reproduce my worries:

1. Create a new “ASP.NET web site” project
2. Add the “ASP.NET folder” called “App_Code” and add a dataset to this folder
3. Set up the datatable adapter to select some columns from e.g. the
Northwind Customers table, for instance the “CustomerID”, “ContactName”, and
“ContactTitle” columns.
4. Add a column to the dataset table and call it “ContactTitleName”. Set the
“Expression” property to “ContactTitle + ' ' + ContactName”.
5. Drag an ObjectDataSource control on to a web page
6. Set up the datasource to get data from your newly created dataset
tableadapter
7. Add a GridView control and set it’s datasource to your newly created
ObjectDataSource
8. Run the project
9. All columns except the expression column called “ContactTitleName” are
rendered

--
Sincerely,

Kjetil Klaussen
Application Developer, Trondheim

Dec 1 '05 #2
Of course! So the GridView isn’t bound to my dataset table, but to my table’s
adapter? That would explain why the expression columns aren’t there :)

Anyway, I found my workaround using a TemplateField column and the standard
late-binding to get my data formatted the way I wanted it;

<Columns>
<asp:TemplateField HeaderText="ContactTitleName"
SortExpression="ContactTitleName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactTitle")+
" " + Eval("ContactName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

--
Sincerely,

Kjetil Klaussen
Application Developer, Trondheim
"Phillip Williams" wrote:
Hi Kjetil,

The ObjectDataSource, in your code below, was using the DataAdapter (which
did not have a definition for the expression column). You needed to fill the
DataTable then bind it to the GridView in order for the expression column to
be used (in the manner you did it), e.g. you needed to add code similar to
this in the CodeFile:

DataSet ds = new DataSet();
DataSetTableAdapters.CustomersTableAdapter adapter1 = new
DataSetTableAdapters.CustomersTableAdapter ();
adapter1.Fill(ds.Customers);
GridView1.DataSource = ds.Customers ;
GridView1.DataBind();

Otherwise, the declarative syntax would have worked, had you defined the new
column in the DataAdapter by modifying the Query:
SELECT CustomerID, ContactName, ContactTitle, ContactTitle+'
'+ContactName as ContactTitleName
FROM Customers

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Kjetil Klaussen" wrote:
Hi,

I’m having some troubles trying to bind my dataset to a GridView control
through an ObjectDataSource control. The binding works fine for regular
columns in my dataset, but I can’t seem to get my expression columns to show
up in my GridView. Anybody knows any neat tricks to make this work?

Here’s a “step-by-step” to reproduce my worries:

1. Create a new “ASP.NET web site” project
2. Add the “ASP.NET folder” called “App_Code” and add a dataset to this folder
3. Set up the datatable adapter to select some columns from e.g. the
Northwind Customers table, for instance the “CustomerID”, “ContactName”, and
“ContactTitle” columns.
4. Add a column to the dataset table and call it “ContactTitleName”. Set the
“Expression” property to “ContactTitle + ' ' + ContactName”.
5. Drag an ObjectDataSource control on to a web page
6. Set up the datasource to get data from your newly created dataset
tableadapter
7. Add a GridView control and set it’s datasource to your newly created
ObjectDataSource
8. Run the project
9. All columns except the expression column called “ContactTitleName” are
rendered

--
Sincerely,

Kjetil Klaussen
Application Developer, Trondheim

Dec 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by perspolis | last post: by
4 posts views Thread by yer darn tootin | last post: by
2 posts views Thread by Steven Nagy | last post: by
2 posts views Thread by =?Utf-8?B?SmF5IFBvbmR5?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.