Dropdown in a gridview column. Visual Studio 2005

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.

Thanks,


This is what I have now.

ASPX

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

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

====
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]"

        dscBrokerAccount.DataBind()

    End Sub

DovbermanAsked:
Who is Participating?
 
RejojohnyConnect With a Mentor Commented:
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 ..

Rejo
0
 
RejojohnyCommented:
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">
                    <ItemTemplate>
                        &nbsp;<asp:Label ID="lblBrokerAccount" runat="server" Text='<%# Eval("AccountID")
%>' >
                        </asp:Label>&nbsp;
                    </ItemTemplate                    
<EditTemplate>
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                            DataTextField="AccountID" DataValueField="AccountKey" Width="127px">
                        </asp:DropDownList>&nbsp;
                    </EditTemplate
0
 
DovbermanAuthor Commented:
Do you mean <EditItemTemplate>
instead of <EditTemplate> ?
0
 
DovbermanAuthor Commented:
Thank you,
The dropdown displays and allows selection.
I need to work on the update command.

Will send feedback later today.
0
 
DovbermanAuthor Commented:
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.

<Columns>
     <asp:CommandField ShowEditButton="True"  ButtonType=Button />
               
<asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
      <ItemTemplate>
                   <asp:Label ID="lblBrokerAccount" runat="server"
                  Text='<%# Eval("BrokerAccount") %>' >
            </asp:Label>&nbsp;
        </ItemTemplate>                    
                   
      <EditItemTemplate>
                  <asp:DropDownList ID="cboBrokerAccount" runat="server" DataSourceID="dscBrokerAccount"
                      DataTextField="AccountID" DataValueField="AccountKey" Width="127px">
                </asp:DropDownList>
        </EditItemTemplate>
</asp:TemplateField>
               
<asp:TemplateField HeaderText="Buy Qty" SortExpression="BuyQty">
                   
      <ItemTemplate>
               <asp:Label ID="lblBuyQty" runat="server" Width="40px"
                  Text='<%# Bind("BuyQty") %>'>
            </asp:Label>
        </ItemTemplate>
                     
      <EditItemTemplate>
               <asp:TextBox ID="txtBuyQty" runat="server"
                              Width="40px"></asp:TextBox>&nbsp;
        </EditItemTemplate>
</asp:TemplateField>
 
      
    '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)
        cmdSQL.ExecuteNonQuery()

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



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.