• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4752
  • Last Modified:

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

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
Dovberman
Asked:
Dovberman
  • 2
  • 2
1 Solution
 
KPMT-TechnicianCommented:
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
 
DovbermanAuthor Commented:
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
 
KPMT-TechnicianCommented:
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
 
DovbermanAuthor Commented:
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
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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now