[Webinar] Streamline your web hosting managementRegister Today

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

Set value in Gridview Dropdownlist for Edit

In my gridview, there are three dropdownlists that appear when the gridview is in edit mode. Each has a textbox associated with it. When the user selects a name, the textbox is populated with the name selected.
When the data is loaded into the dropdown, the value is set to "Select Name".
What I would like to do is this: if a name already exists in the database, I'd like to bind the dropdown to it instead of "Select Name". I'm using a different sqldatasource to select the group of names that need to be in each dropdown.
Alternatively, I could just set the visible property of the dropdown to false if there is already a name in the database.
How can I do this?
I'm posting the source code for the template field.
Thanks for any help
<asp:TemplateField HeaderText="ME" SortExpression="MESig">
            <asp:TextBox runat="server" ID="edMESigTextBox" Text='<%# Bind("MESIG") %>'  ReadOnly="true" Width="100px" SkinID="InsertTB" AutoPostBack="true" />
            <asp:DropDownList runat="server" ID="edMESigDropdownlist" SkinID="DDLInsert2" OnSelectedIndexChanged="changeMETextBox" Width="120px" 
             AutoPostBack="True" DataSourceID="SelectMEName" AppendDataBoundItems="true" DataTextField="FNameLName" DataValueField="FNameLName">            
              <asp:ListItem Text="Select ME Name" Value="" />
              <asp:Label ID="Label6" runat="server" Text='<%# Bind("MESig") %>'></asp:Label>
            <ItemStyle Width="60px" />

Open in new window

1 Solution
This is how I would do...so that you do't have to make database call for filling the dropdownlist for every single row.

1. Create a dropdownlist (ex. drpDummy) on your aspx page outside the gridview and bind it to the "SelectMEName" datasource. Make it invisible.
2. I
3. Inside of the gridview's rowDataBound event, do the following:
 If e.Row.RowType = DataControlRowType.DataRow Then

                   Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)

                    'Create Drop down list for name
                        Dim ddl As DropDownList = CType(e.Row.FindControl("edMESigDropdownlist"), DropDownList)
                        ddl.DataSource = drpDummy.Items
                        ddl.DataTextField = "Text"
                        ddl.DataValueField = "Value"
                        If Not IsDBNull(rowView("TheNameYouWant")) Then
                            ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(CStr(rowView("Status"))))
                        End If
End If

Open in new window

lakhiAuthor Commented:
Please don't close. I was pulled off of this to work on another problem and should be able to get back to it tomorrow.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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