[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dropdown in a gridview column. Visual Studio 2005

Posted on 2007-10-16
5
Medium Priority
?
1,299 Views
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.

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

0
Comment
Question by:Dovberman
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 20086860
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
 

Author Comment

by:Dovberman
ID: 20087135
Do you mean <EditItemTemplate>
instead of <EditTemplate> ?
0
 

Author Comment

by:Dovberman
ID: 20087238
Thank you,
The dropdown displays and allows selection.
I need to work on the update command.

Will send feedback later today.
0
 

Author Comment

by:Dovberman
ID: 20093582
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
 
LVL 26

Accepted Solution

by:
Rejojohny earned 2000 total points
ID: 20095096
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 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