Data Binding in GridView when using ObjectDataSource

Discussion in 'ASP .Net Web Controls' started by SteveM, Apr 1, 2009.

  1. SteveM

    SteveM Guest

    Hi,

    Say you have a GridView control that is sourcing it's data from an
    ObjectDataSource whose select method returns a DataSet. By default, the
    first table in the dataset is the bound to the GridView control.

    I was wondering if there is a way to indicate (either through the
    ObjectDataSource or the GridView) which table should be bound. If the
    GridView is bound directly to a DataSet (through its DataSource property) I
    can use the DataMember property of the Gridview to name the table I want
    bound. Setting the DataMember property, however, throws and exception when
    the Gridview is bound to an ObjectDataSource control.

    I realize that I could just create a method in my undelying business object
    that returns the specific table I want bound and set that method as the
    select method of the ObjectDataSouce. I was just wondering, however, if
    there is a way to do it so that the business object can return a dataset with
    multiple tables and the GridView binds to one other than the first.

    Thanks for any data binding insight you may be able to provide.
     
    SteveM, Apr 1, 2009
    #1
    1. Advertising

  2. Hello Steve,

    this is not directly possible unless you will implement your own DataSource
    control similar to ObjectDataSource. The problem is, that ObjectDataSource is
    just frontend of SINGLE ObjectDataSourceView instance. It means the
    ObjectDataSource only allows you configuring properties of
    ObjectDataSourceView in declarative way. The "View" is what is associated
    with DataMember property in GridView and so on when you use ObjectDataSource
    you can use only its single view = only one DataMember (i guess it is called
    DefautView). The reason for this is that each "View" targets not only Select
    method but also Insert, Update and Delete methods to the DataMember. The
    general idea behind this architecture is that each DataMember contains
    different data objects with different structure so you can hardly generalize
    these methods to accept all of them.

    If you want to only select data from DataSet (without any modification of
    data) you shoud bind return value of your business method directly to
    DataSource property of GridView in code behind and call DataBind on GridView
    (you will not use ObjectDataSource). This will allow you to use DataMember
    property.

    I hope this will help.

    Best regards,
    Ladislav
     
    Ladislav Mrnka, Apr 3, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    6,992
    Guest
    Jun 28, 2005
  2. H5N1
    Replies:
    0
    Views:
    491
  3. =?Utf-8?B?UGhpbGlw?=

    GridView - ObjectDataSource - Data Binding

    =?Utf-8?B?UGhpbGlw?=, Oct 24, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,027
    =?Utf-8?B?UGhpbGlw?=
    Oct 24, 2006
  4. H5N1
    Replies:
    0
    Views:
    152
  5. Jeronimo Bertran

    Binding a GridView to a particular table in an ObjectDataSource

    Jeronimo Bertran, Apr 4, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    209
Loading...

Share This Page