Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Binding ObjdataSource to Gridview

1 view
Skip to first unread message

GaryDean

unread,
Jul 25, 2006, 1:13:27 PM7/25/06
to
I have a GridView that works fine when I bind it to an ObjectDataSource at
design time. But if I bind it at run time nothing happens.

On a button click event I say...

GridView1.DataSourceID = ObjectDataSource1.ID;
GridView1.DataBind();

What's missing?
--
Regards,
Gary Blakely


Walter Wang [MSFT]

unread,
Jul 25, 2006, 9:50:25 PM7/25/06
to
Hi Gary,

Thank you for your post.

This behavior is by design. Normally when you are using declarative
databinding, you set datasource using DataSourceID; when you are setting
datasource at runtime, you set it using DataSource and call DataBind:

GridView1.DataSource = ObjectDataSource1;
GridView1.DataBind();

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (waw...@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

GaryDean

unread,
Jul 26, 2006, 12:54:36 PM7/26/06
to
Walter, again, thanks for responding.

Looks like my binding failue was due to some previous state of the GridView.
after I dropped a fresh GridView your code worked fine.
BUT, that's not really what I am getting at....

I not only want to bind in code but I want to set all the Enable Editing,
Deleting, Selection, Paging, Sorting features in code but I can't see how to
EnableEditing at run time.

I tried to drop a new GridView on the page and then, at design time, added
the Edit and Delete buttons. then bound it at run time but when I hit an
Edit button I got an exception saying "GridView1 fired event RowEditing
which was not handled.

The answer to WHY do I want to do this is because I would like to have one
GridView on the form that will update a number of datasources depending upon
selections by the user.

--
Regards,
Gary Blakely
"Walter Wang [MSFT]" <waw...@online.microsoft.com> wrote in message
news:g%23b%23jXFsG...@TK2MSFTNGXA01.phx.gbl...

Walter Wang [MSFT]

unread,
Jul 26, 2006, 11:24:29 PM7/26/06
to
Hi Gary,

Thank you for your update.

To perform v1-style databinding and editing using GridView, you need to
handle the ItemCommand event to capture the Edit command (just like you
handled the EditCommand event using DataGrid). You also need to re-databind
the GridView after changing the EditIndex in order to cause the grid to
re-render the data in edit mode. Same as DataGrid.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs
e) {
if (e.CommandName == "Edit") {
GridView1.EditIndex = Int32.Parse(e.CommandArgument.ToString());
GridView1.DataSource = ObjectDataSource1;
GridView1.DataBind();
}
}

Also, you must handle many required events when setting DataSource at
runtime.

However, you could probably avoid all this work by simply making your
DataSource available through an ObjectDataSource control and then
databinding the GridView using DataSourceID instead of the v1-style
approach.

Since you mentioned you're using one GridView to update several
DataSources, I suppose you're still updating a DataSource at one time.

I suggest you to use one ObjectDataSource, but create the Select, Delete,
Update, Insert methods with one additional parameter to distinguish the
user's selection. When you declare the ObjectDataSource in ASPX, you can
use the <UpdateParameters>, <DeleteParameters>, etc. to include a
ControlParameter which references the control that represents user's
selection. In ObjectDataSource's methods, you will know which data source
is expected to access.

Walter Wang [MSFT]

unread,
Jul 26, 2006, 11:30:21 PM7/26/06
to
Some sample code (only update method is demostrated here):

<asp:DropDownList ID="ddlChoice" runat="server"></asp:DropDownList>
<asp:ObjectDataSource TypeName="MyNamespace.MyOds"
UpdateMethod="UpdateData" ID="ods1" runat="server">
<UpdateParameters>
<asp:ControlParameter Name="dataSourceType"
ControlID="ddlChoice" PropertyName="SelectedValue" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>

Suppose the method MyOds.UpdateData() has first parameter set to "int
dataSourceType". And the DropDownList's SelectedValue property is binding
to an Int32 which represents selected dataSourceType.

Walter Wang [MSFT]

unread,
Jul 31, 2006, 2:59:52 AM7/31/06
to
Hi Gary,

Please feel free to post here if anything is unclear or you need more
detailed sample code.

0 new messages