473,414 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Binding ObjdataSource to Gridview

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
Jul 25 '06 #1
5 2783
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 (wa****@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.

Jul 26 '06 #2
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]" <wa****@online.microsoft.comwrote in message
news:g%******************@TK2MSFTNGXA01.phx.gbl...
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 (wa****@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.

Jul 26 '06 #3
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 (wa****@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.

Jul 27 '06 #4
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 (wa****@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.

Jul 27 '06 #5
Hi Gary,

Please feel free to post here if anything is unclear or you need more
detailed sample code.
Regards,
Walter Wang (wa****@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.

Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument"...
3
by: Hans Merkl | last post by:
Hi, I was wondering if it's possible to bind the header text of a GridView column to a method of an object I have. At the moment I am setting the header texts in Page_Load but I was wondering if...
1
by: Smash | last post by:
I have problem binding gridview.... At first i didn't want to bind gridview at first page load...so in my objectdatasource_selecting event i wrote this: If Not Page.IsPostBack Then e.Cancel =...
3
by: mateo | last post by:
Hello, I have a GridView inside which i want to show 3 dropdownList (one for each Column). So i've created 3 TemplateField Continent Country City
5
by: Amit | last post by:
Hello, I have a simple search screen, with two drop-downs and a text box. There's also a GridView control that is using a SqlDataSource control to show the matching results. The SqlDataSource uses...
8
by: AG | last post by:
ASP.NET 2.0, VS 2005 I have a gridview with paging enabled, bound to an objectdatasource. Both were dropped on to the page and configured via the wizards. Basically working as expected. The...
4
by: Mike | last post by:
I'm having trouble getting a gridview to bind. I probably missing something completely obvious and would appreciate any help on offer. I'm passing parameters via querystring, and have created a...
3
by: RobertTheProgrammer | last post by:
Hi folks, I've got another problem. Basically, I'm trying to use a nested GridView, however the nexted GridView displays no values (even though in debug I'm getting valid values into my DataSet. ...
0
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.