Selecting New Row in Gridview with an ObjectDataSource

Discussion in 'ASP .Net Web Controls' started by Jay Pondy, Apr 2, 2007.

  1. Jay Pondy

    Jay Pondy Guest

    I have a Gridview with Paging and Sorting turned on that is bound to an
    ObjectDataSource which in turn is bound to a StronglyTyped datatable.

    After I add a new row to my Stronglye Typed datatable and call
    GridView.DataBind how can I select the row in the GridView? (And I don't mean
    set the Grid.PageIndex and Grid.SelectedIndex but rather how can I determine
    where in the SORTED result set the new row is.)

    In the DataSourceObject Selected event I can cast the ReturnValue to my
    strongly typed datatable BUT it is not yet sorted. Tracing the code shows
    that the GridViews Sorting and Sorted Events have already fired so I am a bit
    confused as to what is happening when. Somehow I need to set the GridViews
    PageIndex and SelectedIndex based on where the new row is in the SORTED
    result set.

    Seems like I'm close but after two days I'm feeling a bit frustrated and
    wondering again if this data binding stuff is worth the trouble. It sure
    seems like ASP 2.0 is very close to making it worth the trouble so onward I
    shall press.
    Jay Pondy, Apr 2, 2007
    #1
    1. Advertising

  2. Hi Jay,

    From your description, you have a GridView that bound to
    ObjectDataSource(which use typed dataset/datatable to supply datarows).
    However, since you've turned on paging and sorting on the GridView, you
    found it difficult to get the Index of a certain datarow in the GridView
    and make it be selected, correct?

    As for this issue, I have performed some test on my side. And here are what
    I found so far:

    ** For GridView, when enable paging and sorting, the paged and sorted data
    resultset are generated internally, that's why we can not get the paged &
    sorted result set in the ObjectDataSource control's event. Also, GridView
    doesn't expose interface for us to intercept this result set.

    ** For the selected Row, we need to set the GridView.SelectedIndex before
    databinding is performed, we can not set this during databinding or when
    row is creating or binding....

    Currently, what I can get is the following approachs to make a certain
    datarecord you want be selected in Gridview:

    1)Use the GridView.RowDataBound event to get the expected row's RowIndex
    (save it in a certain variable for later use) e.g.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
    e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    Response.Write("<br/>" + e.Row.RowIndex);

    DataRowView drv = e.Row.DataItem as DataRowView;

    if (drv["name"].ToString() == "name1")
    {
    //save the RowIndex here
    }
    }

    }

    2) Then, after GridView has finished the first databinding, you can set the
    RowIndex(saved previously) to GridView.SelectedIndex and call Databind to
    redo the databinding. This drawback is that you'll need to redo the
    databinding.

    How do you think? If you have any further questions, please feel free to
    post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Apr 3, 2007
    #2
    1. Advertising

  3. Jay Pondy

    Jay Pondy Guest

    Appreciate your time and effort on this rather perplexing problem.

    The problem with using the GridView1_RowDataBound event is that it only
    fires for the rows that the grid will currently be displaying rather than for
    all of the rows in the result set. If it fired for all the rows your
    solution would work.

    As an example if my grid is sorted and I am on page 1 and I add a record
    that should appear on page 2 I will never see the new record in the
    RowDataBound event because it belongs on page 2. Since I never see it I can
    not determine it's RowIndex.


    "Steven Cheng[MSFT]" wrote:

    > Hi Jay,
    >
    > From your description, you have a GridView that bound to
    > ObjectDataSource(which use typed dataset/datatable to supply datarows).
    > However, since you've turned on paging and sorting on the GridView, you
    > found it difficult to get the Index of a certain datarow in the GridView
    > and make it be selected, correct?
    >
    > As for this issue, I have performed some test on my side. And here are what
    > I found so far:
    >
    > ** For GridView, when enable paging and sorting, the paged and sorted data
    > resultset are generated internally, that's why we can not get the paged &
    > sorted result set in the ObjectDataSource control's event. Also, GridView
    > doesn't expose interface for us to intercept this result set.
    >
    > ** For the selected Row, we need to set the GridView.SelectedIndex before
    > databinding is performed, we can not set this during databinding or when
    > row is creating or binding....
    >
    > Currently, what I can get is the following approachs to make a certain
    > datarecord you want be selected in Gridview:
    >
    > 1)Use the GridView.RowDataBound event to get the expected row's RowIndex
    > (save it in a certain variable for later use) e.g.
    >
    > protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
    > e)
    > {
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    > Response.Write("<br/>" + e.Row.RowIndex);
    >
    > DataRowView drv = e.Row.DataItem as DataRowView;
    >
    > if (drv["name"].ToString() == "name1")
    > {
    > //save the RowIndex here
    > }
    > }
    >
    > }
    >
    > 2) Then, after GridView has finished the first databinding, you can set the
    > RowIndex(saved previously) to GridView.SelectedIndex and call Databind to
    > redo the databinding. This drawback is that you'll need to redo the
    > databinding.
    >
    > How do you think? If you have any further questions, please feel free to
    > post here.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    Jay Pondy, Apr 3, 2007
    #3
  4. Thanks for your reply Jay,

    Yes, you're right. When using paging , only the displayed page's item will
    go through the created/bound event sequence. Another approach I can get is
    manually calcualte the index(include paging context) based on GridView's
    current SortExpression and SortDirection. You can get GridView's updated
    "SortExpression" and "SortDirection" in ObjectDataSource.Selected event.
    e.g.

    protected void ObjectDataSource1_Selected(object sender,
    ObjectDataSourceStatusEventArgs e)
    {
    Response.Write("<br/>ObjectDataSource1_Selected");

    Response.Write("<br/>" + GridView1.SortExpression);
    Response.Write("<br/>" + GridView1.SortDirection);
    }

    Then, you can try manually creating a DataView with sort setting conform to
    the GridView's "sortExpression" and "SortDirection". After that you can
    loop the DataRowView in the DataView to locate the index of the row you
    want. This index should identitcal to the index after GridView have bound
    to the Sorted the result set. For example:

    ==================
    protected void ObjectDataSource1_Selected(object sender,
    ObjectDataSourceStatusEventArgs e)
    {
    Response.Write("<br/>ObjectDataSource1_Selected");
    Response.Write("<br/>" + GridView1.SortExpression);
    Response.Write("<br/>" + GridView1.SortDirection);

    _tb = e.ReturnValue as DataTable;

    if (_tb != null && GridView1.SortExpression != string.Empty)
    {

    DataView dv = new DataView(_tb);
    dv.Sort = GridView1.SortExpression + " ASC";

    // check the index of the expected row in the dataview

    }
    }
    ======================

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Apr 4, 2007
    #4
  5. Jay Pondy

    Jay Pondy Guest

    Steven

    That will certainly work and pretty much takes me back to where I left off.
    It just that it doesn't seem right to have the tools (objectDataSource /
    GridView) do the job and then have to do it again manually to get what you
    want done.

    What gripes me the most is the "you don't have to write any code or very
    little code" which to some extent is true - some of these tools can save you
    a lot of code BUT more often then not it seems I find myself trying to beat
    the tools into submission to do things the way I want them done.

    You would think that after all the million man hours MS has put into
    databinding since the VBx days that somebody would recognize that users
    really do like to see what they've just done. More than any other post
    concering the GridView is the classic "how do I select the row".

    Thanks for your help and all the great tips you have contributed to these
    Newsgroups over the years. I genuinely appreciate them.

    "Steven Cheng[MSFT]" wrote:

    > Thanks for your reply Jay,
    >
    > Yes, you're right. When using paging , only the displayed page's item will
    > go through the created/bound event sequence. Another approach I can get is
    > manually calcualte the index(include paging context) based on GridView's
    > current SortExpression and SortDirection. You can get GridView's updated
    > "SortExpression" and "SortDirection" in ObjectDataSource.Selected event.
    > e.g.
    >
    > protected void ObjectDataSource1_Selected(object sender,
    > ObjectDataSourceStatusEventArgs e)
    > {
    > Response.Write("<br/>ObjectDataSource1_Selected");
    >
    > Response.Write("<br/>" + GridView1.SortExpression);
    > Response.Write("<br/>" + GridView1.SortDirection);
    > }
    >
    > Then, you can try manually creating a DataView with sort setting conform to
    > the GridView's "sortExpression" and "SortDirection". After that you can
    > loop the DataRowView in the DataView to locate the index of the row you
    > want. This index should identitcal to the index after GridView have bound
    > to the Sorted the result set. For example:
    >
    > ==================
    > protected void ObjectDataSource1_Selected(object sender,
    > ObjectDataSourceStatusEventArgs e)
    > {
    > Response.Write("<br/>ObjectDataSource1_Selected");
    > Response.Write("<br/>" + GridView1.SortExpression);
    > Response.Write("<br/>" + GridView1.SortDirection);
    >
    > _tb = e.ReturnValue as DataTable;
    >
    > if (_tb != null && GridView1.SortExpression != string.Empty)
    > {
    >
    > DataView dv = new DataView(_tb);
    > dv.Sort = GridView1.SortExpression + " ASC";
    >
    > // check the index of the expected row in the dataview
    >
    > }
    > }
    > ======================
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    Jay Pondy, Apr 4, 2007
    #5
  6. Thanks for your reply Jay,

    Yes, in ASP.NET 2.0, many new encapsulated controls such as Gridview,
    DetailsView, SqlDataSource has made the databinding codeless (just
    drag/drop and configure), however the more encapsulated, the less
    flexibility we will have. That's make us a bit hard to do many
    customization upon those encapsulated controls. Actually, for those cases
    we need more flexibility and customization, the ASP.NET 1.1 like
    databinding model(through code) is the preferred one.

    For this issue here, maybe programmatic databinding would be better.
    Anyway, I think it a good idea that the GridView (and any other well
    encapsulated controls) also provide more customization interfaces, and
    you're welcome to submit such feature requests to our product feedback
    center:

    http://connect.microsoft.com/feedback/default.aspx?SiteID=210

    As always, whenever you meet any problems that need help, please feel free
    to post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Apr 5, 2007
    #6
    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. Chip

    Selecting a row within a row

    Chip, Feb 10, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    355
  2. misiek
    Replies:
    0
    Views:
    2,267
    misiek
    Feb 20, 2006
  3. H5N1
    Replies:
    0
    Views:
    2,700
  4. Replies:
    5
    Views:
    649
  5. Not Me
    Replies:
    0
    Views:
    345
    Not Me
    Jan 26, 2007
Loading...

Share This Page