Binding ObjdataSource to Gridview

G

GaryDean

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?
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
G

GaryDean

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.
 
W

Walter Wang [MSFT]

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.

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



Regards,
Walter Wang ([email protected], 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.
 
W

Walter Wang [MSFT]

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.


Regards,
Walter Wang ([email protected], 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.
 
W

Walter Wang [MSFT]

Hi Gary,

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


Regards,
Walter Wang ([email protected], 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top