Gridview - How to save the selected value from a drop down list?

Last post 10-05-2007 12:10 AM by noslenj123. 7 replies.

Sort Posts:

  • Gridview - How to save the selected value from a drop down list?

    10-03-2007, 7:56 PM
    • Loading...
    • noslenj123
    • Joined on 05-20-2007, 6:34 PM
    • Posts 14

     My gridview has an EditItemTemplate with a dropdown.  The dropdown is populated no problem and the correct value is displayed when in edit mode.  However, I haven't figured out how a changed dropdown selection will save to the database when clicking the "update" button.

    This gets me populated just fine. 

                                    <EditItemTemplate>
                                        <asp:DropDownList runat="server" ID="ddPeriods" DataSourceID="dsPeriods" DataTextField="period_name" DataValueField="period_id" SelectedValue='<%# eval("period_id") %>'></asp:DropDownList>
                                    </EditItemTemplate>
     

    I'm guessing I need to update the rows' data somehow, possibly in the RowUpdating event? Can anyone point me to how to retrieve the dropdowns value and how to populate the data field before the update is issued to the database?

  • Re: Gridview - How to save the selected value from a drop down list?

    10-03-2007, 9:56 PM
    • Loading...
    • sharma.pushp
    • Joined on 05-29-2007, 4:44 AM
    • Melbourne, Australia
    • Posts 121

     Hi..

     

    Your "Edit template" seems correct to me,

    have you binded gridview to some sqldatasource. If yes check whether you have correct update command there. 

    Other wise if u binded it with some other source for exeample Object data source, then u have to write some update function for it.

    Cheers
     

    Pushp

    If you find this post useful, mark it as an Answer
  • Re: Gridview - How to save the selected value from a drop down list?

    10-03-2007, 10:45 PM
    • Loading...
    • djh
    • Joined on 06-09-2006, 6:08 AM
    • Posts 65

    Hey Nos,

    First thing you should try is changing 'eval' to 'bind'.

    To put it very (very) simply:
    'Eval' is one-way binding (read only).
    'Bind' is two-way binding (read/write).

    Try it like this:
    SelectedValue='<%# bind("period_id") %>'

    Hope this helps,
    DJH

  • Re: Gridview - How to save the selected value from a drop down list?

    10-04-2007, 12:22 AM
    • Loading...
    • noslenj123
    • Joined on 05-20-2007, 6:34 PM
    • Posts 14

     Pushp,  I did bind my dropdown to a different data source so like you said, I probably need to write something somewhere to get the data into the grid before the update.  Of course, that is my question, where do I do that and how do I get hold of the objects needed.

     DJH, thanks for the information regarding "bind" vs "eval", that makes sense and I actually held out a little hope that the change might make it work.  But alas, no go. 

     Anyone know how to update the gridview data field from the dropdown that has a seperate data source?
     

  • Re: Gridview - How to save the selected value from a drop down list?

    10-04-2007, 12:35 AM
    Answer

    Hi noslenj123 ,

    Asuming that you have bound the GirdView to same DataSourceID that is dsPeriods. Here should be your code behind.

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim DropDownPeriods As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddPeriods"), DropDownList)

     

    e.NewValues.Item("period_id") = DropDownPeriods.SelectedValue

     

    End Sub

    I hope it helps.

    Regards,

    Harish

    If you find this post useful, mark it as an Answer

    -Harish
  • Re: Gridview - How to save the selected value from a drop down list?

    10-04-2007, 8:30 AM
    • Loading...
    • noslenj123
    • Joined on 05-20-2007, 6:34 PM
    • Posts 14

    Harish,

     You are da man!  I was getting closer and closer to your solution as I researched but you nailed it on the head.  I may actually have this part of code working enough to demo to our client this morning.  Thanks so much!!
     

  • Re: Gridview - How to save the selected value from a drop down list?

    10-04-2007, 11:51 PM

    Thank you noslenj123. Please mark this post as answer.

    Best of luck for your project!!

    If you find this post useful, mark it as an Answer

    -Harish
  • Re: Gridview - How to save the selected value from a drop down list?

    10-05-2007, 12:10 AM
    • Loading...
    • noslenj123
    • Joined on 05-20-2007, 6:34 PM
    • Posts 14

    Actually Harish I did already mark your answer as the one.  I see no option now so I hope you have the credit!!!!

     

    Joel 

Page 1 of 1 (8 items)
Microsoft Communities
Page view counter