Dropdown in a gridview column. Visual Studio 2005

Posted on 2007-10-16
Last Modified: 2013-11-26
Dropdown in a gridview column

I need a dropdown control to display when the Edit button is clicked.
When a selection is made from the dropdown and the user clicks 'Update',
I need to display the selected value in a read only textbox.

Currently the dropdown displays both before and after the Edit button is clicked.

Any ideas or references would be helpful.


This is what I have now.


                <asp:CommandField ShowEditButton="True"  ButtonType=Button />
                <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                            DataTextField="AccountID" DataValueField="AccountKey" Width="127px">

 <asp:SqlDataSource ID="dscBrokerAccount" runat="server" ConnectionString="<%$ ConnectionStrings:StockSelectConnectionString %>"
                  SelectCommand="SELECT [AccountID], [AccountKey] FROM [Account] WHERE ([UserID] = @UserID) ORDER BY [AccountID]  ">
                 <asp:ControlParameter ControlID="txtGridParam" Name="UserID" PropertyName="Text"
                     Type="Object" />

Code Behind

Protected Sub grdPurchases_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdPurchases.RowEditing

        Dim intRowIndex As Integer

        intRowIndex = e.NewEditIndex 'Row being edited
        'Set selected row to the row being edited
        grdPurchases.SelectedIndex = intRowIndex

        Dim cboBrokerAccount As DropDownList = CType(grdPurchases.Rows(intRowIndex).FindControl("cboBrokerAccount"), DropDownList)

        dscBrokerAccount.SelectCommand = _
        "SELECT [AccountID], [AccountKey] FROM [Account] " _
        & "WHERE [UserID] ='" & txtGridParam.Text & "' ORDER BY [AccountID]"


    End Sub

Question by:Dovberman
    LVL 26

    Expert Comment

    you will need to replace the dropdown in the itemtemplate with a label .. and move the dropdown to the edit template ..
    <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
                            &nbsp;<asp:Label ID="lblBrokerAccount" runat="server" Text='<%# Eval("AccountID")
    %>' >
                            &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                                DataTextField="AccountID" DataValueField="AccountKey" Width="127px">

    Author Comment

    Do you mean <EditItemTemplate>
    instead of <EditTemplate> ?

    Author Comment

    Thank you,
    The dropdown displays and allows selection.
    I need to work on the update command.

    Will send feedback later today.

    Author Comment

    I am having trouble updating.

    The edititemtemplate behaves as expected. I can select a value from the dropdown and enter a value for the BuyPrice.

    How do I read the newvalues?

    Currently, I am hard coding the newvalues just to test the UpdateCommand.

         <asp:CommandField ShowEditButton="True"  ButtonType=Button />
    <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
                       <asp:Label ID="lblBrokerAccount" runat="server"
                      Text='<%# Eval("BrokerAccount") %>' >
                      <asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                          DataTextField="AccountID" DataValueField="AccountKey" Width="127px">
    <asp:TemplateField HeaderText="Buy Qty" SortExpression="BuyQty">
                   <asp:Label ID="lblBuyQty" runat="server" Width="40px"
                      Text='<%# Bind("BuyQty") %>'>
                   <asp:TextBox ID="txtBuyQty" runat="server"
        'Get values of grid columns that are not edited

           Dim intSymbolID As Integer = Convert.ToInt32(grdPurchases.Rows(intRowIndex).Cells(8).Text)
            Dim intBuyTransID As Integer = Convert.ToInt32(grdPurchases.Rows(intRowIndex).Cells(9).Text)
            Dim dteBuyDate As Date = calBuyDate.SelectedDate

       'Get the new values
            '??? Dim BrokerAccount as the dropdown selected value  'How?
            ' ??? Dim intBuyQty As Integer = ??? How do I get the new value?
            Following are not in itemedittemplates, but they need to be.
            Dim decBuyPrice As Decimal = Convert.ToDecimal(e.NewValues.Item(0))
            Dim decBuyComm As Decimal = Convert.ToDecimal(e.NewValues.Item(1))
            Dim blnBuyCompleted As Byte = Convert.ToByte(e.NewValues.Item(2))

           'Update the Purchases table
    'Cannot read the newvalue of BrokerAccount from the dropdown. Hard coded to 'abc'

    'Cannot read the newvalue of BuyQty. Hard coded to 5.

          strUpdateSQL = "UPDATE [Purchases]
               SET [SymbolID] = " & intSymbolID & ", " _
                 & "[BrokerAccount] = 'abc', [BuyQty] = 5, " _
                 & "[BuyPrice] = " & decBuyPrice & ",  [BuyDate] = '" & dteBuyDate & "'," _
                 & "[BuyComm] = " & decBuyComm & ", [BuyCompleted] = " & blnBuyCompleted & " " _
                 & "WHERE [BuyTransID] = " & intBuyTransID & " "

          cmdSQL = New SqlCommand(strUpdateSQL, conStockSelector)

    Actual SQL Statement. Update works, but had to hardcode the BuyQty and BrokerAccount
    UPDATE [Purchases] SET [SymbolID] = 18617, [BrokerAccount] = 'abc',
    [BuyQty] = 5, [BuyPrice] = 43.80,  
    [BuyDate] = '10/10/2007',[BuyComm] = 0.00, [BuyCompleted] = 0
    WHERE [BuyTransID] = 47

    LVL 26

    Accepted Solution

    How do I read the newvalues?
    find the dropdownlist using findcontrol and use the control's "selectedvalue" property

    Sorry for not reponding to some of your earlier comments .. i did not get the mail from EE for your response and so did not check this thread ..


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now