?
Solved

Set SelectedValue of DropdownList in EditItemTemplate of Gridview. Visual Studio 2005.

Posted on 2007-10-20
4
Medium Priority
?
4,739 Views
Last Modified: 2013-11-26
Set SelectedValue of DropdownList in EditItemTemplate of Gridview. Visual Studio 2005.

When the gridview row Edit button is clicked, the DropDownList displays and is set to the selected value of the first row of the DropDownList SQL statement.

I need to set the selected value to the old value.
The label control of the Gridview contains the old value.

How can I set the selected value to the old value ?

This is my code:

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

'Set the selected value of the DropDownList to the OldValue
????
   
    End Sub
0
Comment
Question by:Dovberman
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:KPMT-Technician
ID: 20115098
Sounds like you have the datagrid dropdownlist column converted into a temple column already.  Also, already have a DataSource bound to the that dropdownlist.  So in design view edit the template column with the dropdownlist and bind the selectedvalue property of the dropdownlist to the value of the column.  There is know need to do this on the rowEditing event.

IE....
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#  Bind("myColumnValue") %>' />

Robert Fidler
Springfield, Missouri
0
 

Author Comment

by:Dovberman
ID: 20117539
I have tried that before with no success.

 <Columns>
                <asp:CommandField ShowEditButton="True"  ButtonType=Button />
                <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
                   <ItemTemplate>
                        &nbsp;<asp:Label ID="lblBrokerAccount" runat="server"
                        Text='<%# Eval("AccountID") %>'>
                    </asp:Label>&nbsp;
                    </ItemTemplate>                    
                    <EditItemTemplate>
                        &nbsp;<asp:DropDownList ID="cboBrokerAccount" runat="server"
                            DataSourceID="dscBrokerAccount"
                            DataTextField="AccountID" DataValueField="AccountKey"
                            Width="127px"
                            SelectedValue='<%#Bind("AccountKey") %>'>

                        </asp:DropDownList>
                    </EditItemTemplate>
                   
                </asp:TemplateField>

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'AccountID'.

Source Error:

Line 139:                <asp:TemplateField HeaderText="Broker Acct" SortExpression="BrokerAccount">
Line 140:                   <ItemTemplate>
Line 141:                        &nbsp;<asp:Label ID="lblBrokerAccount" runat="server"
Line 142:                        Text='<%# Eval("AccountID") %>'>
Line 143:                    </asp:Label>&nbsp;
 
0
 
LVL 5

Accepted Solution

by:
KPMT-Technician earned 2000 total points
ID: 20120705
Looks like to data your binding to doesn't have a column ''AccountID''.  I'd start looking there.

If all else fails email me the aspx and the table stucture for your data and I'll take a look at it.

Robert Fidler
Springfield, Missouri

email address is: rfidler at kpmtechnology dot com
0
 

Author Comment

by:Dovberman
ID: 20122850
Thanks Robert,

The data binding expressions no longer cause an error.

I will send you more details and increase the point value to 500.

However, attempts to FindControl of a control in the EditItemTemplate fail.

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

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

'lstAccount is Nothing
        Dim intSelAccount As Integer = 1

        ' Get the row being edited.
        Dim rowSel As GridViewRow = grdPurchases.Rows(e.NewEditIndex)

        'Populate the Account DDL

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

        lstAccount.DataBind()

    End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 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