Expression Columns problem when using ObjectDataSource

G

Guest

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
 
G

Guest

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
 
G

Guest

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>
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top