locked
Can I change the selected row after using an FK filter? RRS feed

  • Question

  • User-931283679 posted

    This must be a simple question but I can't seem to find the answer anywhere!

    I am DD2 with LINQ-to-SQL and have a standard table with an FK filter drop-down.  Initially, the first row is selected and its details are displayed in the view beneath the table:

    Image showing a table with the first row selected

    However, if I use the filter, no (new) row is selected; more importantly, the old details are still showing beneath the table:

    Image showing a table with no row selected

    Presumably this is because I need to manually select a new row, so I tried this code:

        protected void GridView1_DataBound(object sender, EventArgs e) {
            if (GridView1.Rows.Count == 0) {
                FormView1.ChangeMode(FormViewMode.Insert);
            }
            else
            {
                // after rebinding the data, automatically set the first row as Selected
                GridView1.SelectedIndex = 0;
            }
        }
    

    The selection part worked (in that the first row of the filtered table is highlighted in yellow) but the details below the table are not always updated (sometimes they are, sometimes they're not!).

    Any advice gratefully received!

    Ed Graham

    Thursday, January 5, 2012 8:31 AM

Answers

  • User-931283679 posted

    Thanks Steve.  Interestingly, that works exactly half the time!  In other words, for every two times I change the selected item in the drop-down filter, the details view (FormView) beneath the table is updated on the second change only.  Still, it's better than it was before, so thanks again.  (The row in the GridView itself is highlighted in yellow every time, but it wasn't that I was worried about.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 12, 2012 7:52 AM

All replies

  • User-330204900 posted

    What you are seeing where no row appears to be selected is not what it appears the an item is still selected but the filter prevents it being shown. If you debug you will see the in either case the same PK is selected.

    Thursday, January 5, 2012 3:45 PM
  • User-931283679 posted

    What you are seeing where no row appears to be selected is not what it appears the an item is still selected but the filter prevents it being shown. If you debug you will see the in either case the same PK is selected.

    Thanks for that, Steve.  Is there some way that I can change the selected PK to be the first row shown in the filtered table and (most importantly) have the new details show in the details view beneath the table?  If this isn't clear, give me a shout and I'll provide some pictures to show what I mean.

    Friday, January 6, 2012 5:33 AM
  • User-931283679 posted

    What you are seeing where no row appears to be selected is not what it appears the an item is still selected but the filter prevents it being shown. If you debug you will see the in either case the same PK is selected.

    Thanks for that, Steve.  Is there some way that I can change the selected PK to be the first row shown in the filtered table and (most importantly) have the new details show in the details view beneath the table?  If this isn't clear, give me a shout and I'll provide some pictures to show what I mean.

    Just nudging this one back up the list ...

    Tuesday, January 10, 2012 7:47 PM
  • User-330204900 posted

    Hi Ed, I'll try that for you

    Wednesday, January 11, 2012 1:53 PM
  • User-330204900 posted

    What I did was add this;

    if (GridView1.SelectedIndex == -1)
    {
        GridView1.SelectedPersistedDataKey = table.GetDataKeyFromRoute();
        if (GridView1.SelectedPersistedDataKey == null)
            GridView1.SelectedIndex = 0;
    }

    to the GridView1_DataBound event like this:

     

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count == 0)
            FormView1.ChangeMode(FormViewMode.Insert);
    
        if (GridView1.SelectedIndex == -1)
        {
            GridView1.SelectedPersistedDataKey = table.GetDataKeyFromRoute();
            if (GridView1.SelectedPersistedDataKey == null)
                GridView1.SelectedIndex = 0;
        }
    }

     your issue is that where you were trying to set the SelectedIndex was too late int he page life cycle

    Wednesday, January 11, 2012 2:12 PM
  • User-931283679 posted

    Thanks Steve.  Interestingly, that works exactly half the time!  In other words, for every two times I change the selected item in the drop-down filter, the details view (FormView) beneath the table is updated on the second change only.  Still, it's better than it was before, so thanks again.  (The row in the GridView itself is highlighted in yellow every time, but it wasn't that I was worried about.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 12, 2012 7:52 AM
  • User-330204900 posted

    Hi Ed, the issue here catching the DDL change early enough to cause the GridView to show the changed value, you may need to mess around with the which event you put this in and maybe remove the setting from others.

    Thursday, January 12, 2012 8:06 AM
  • User-330204900 posted

    HI Ed, I found the solution you need to add this:

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex == -1)
        {
            GridView1.SelectedPersistedDataKey = table.GetDataKeyFromRoute();
            if (GridView1.SelectedPersistedDataKey == null)
                GridView1.SelectedIndex = 0;
        }
    }

    and hook up the event to the gridview and remove

    if (GridView1.SelectedIndex == -1) 
    { 
        GridView1.SelectedPersistedDataKey = table.GetDataKeyFromRoute(); 
        if (GridView1.SelectedPersistedDataKey == null) 
            GridView1.SelectedIndex = 0; 
    }

     from the GridView1_DataBound event and all will work fine.

     

    Thursday, January 12, 2012 8:19 AM
  • User-931283679 posted

    Thanks very much for trying that for me Steve; very good of you!  I actually get the same result as when I had the code in the OnDataBound event handler; that is, the FormView is updated for every second selection (rather than every selection as you would expect).  As a work-around, I changed the code so that the FormView is hidden (Visibility = false) in the DataBound handler, and set to visible in the Page_Load handler; that way there won't be any stale data on show in the FormView (as the user will have to explicitly select a row if he wants to see it).

    Monday, January 23, 2012 6:07 AM
  • User-330204900 posted

    Would you like me to send you the working sample? my sample works fine...

    Monday, January 23, 2012 9:36 AM
  • User-931283679 posted

    Would you like me to send you the working sample? my sample works fine...

    Very kind of you, Steve: thanks!  My boss is happy with my work-around, so I should probably leave it for now; much appreciated, though.  There's obviously something silly that I'm doing wrong somewhere ... Smile

    Wednesday, February 1, 2012 3:32 PM
  • User-330204900 posted

    OK Ed no problems just drop me an e-mail when you want the sample sending :)

    Wednesday, February 1, 2012 5:07 PM