GridView

G

Guest

How can I update a record from a dropdownlist in a gridview?

<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="status" runat="server">
<%#Eval("status")%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server">
<asp:ListItem Value="A">Active</asp:ListItem>
<asp:ListItem Value="M">Moved</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
 
G

Guest

1- Handle the GridView.RowUpdating event
Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)

2- find the updated row:
Dim index As Integer = GridView1.EditIndex 'or you can use e.RowIndex
Dim row As GridViewRow = GridView1.Rows(index)

3- find the dropdownlist control (which you must give an ID in the markup,
let's say ddlStatus)
Dim ddl as DropDownList = row.FindControl("ddlStatus")

4- set the new value:
e.NewValues("status") = ddl.SelectedValue

Another approach is to handle the SelectedIndexChanged of the dropdownlist
to copy the selectedValue to a TextBox that is styled as hidden within the
same template. The textbox should be 2-way databound using Bind to the field
named "status".
 
K

Kim Quigley

This is how I did it:

-Go to the drop down list's smart tag and click on "Edit DataBindings."
-For the SelectedValue property, select an item from the "Bound to" list
that you want to bind to. In your case it would be the status field.
-Make sure two way databinding is checked.

If the Field Binding radio button is grayed out, then click Refresh Schema.
 
G

Guest

This approach would work fine as long as the underlying data field always
contains "A" or "M". If the database table allows for null values to be
stored you probably would have to add a blank entry to the dropdownlist
otherwise you would get an exception that the selected value does not exist
in the list.
<asp:DropDownList runat="server">
<asp:ListItem Value="">No Selection made</asp:ListItem>
<asp:ListItem Value="A">Active</asp:ListItem>
<asp:ListItem Value="M">Moved</asp:ListItem>
</asp:DropDownList>

In other words you have to match the dropdownlist items to the possible data
values that can be stored in the status field. If the status field can
contain other values then an exception would be thrown.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top