Creating a dropdown in gridview control

Posted by Preddy308 under C# on 4/26/2011 | Points: 10 | Views : 3301 | Status : [Member] | Replies : 2

I got to insert a dropdown in the gridview and need to update some data when the value from dropdown in the gridview is selected.

My question is : what event needs to fired to populate data related to the value selected from the dropdown...

I tried databound...but I donno how to find the control(dropdown) in this event.

Can anybody please help me with this issue.



Posted by: Ndebata on: 4/26/2011 [Member] Starter | Points: 25


If you want to do some operation upon selection of a value in dropdown list, use selectedindexchanged event with autopostback set as true.

From the sender parameter you can get the gridview row containing your dropdown.

DropDownList dlist=currentgridrow.FindControl("controlid") as Dropdownlist

Take a look at the attached sample.
 Download source file

Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srinivas673 on: 4/27/2011 [Member] Starter | Points: 25


In .aspx Page
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>' />
<asp:DropDownList ID="ddlstatus" CssClass="changedropdown" runat="server">
<img src="../../Themes/Images/reg-alert-icon.png" class="alert" id="imgeditstatus" style="margin-left: 5px;
float: right" alt="manditory" />
<asp:Label ID="lblNewStatus" runat="server" Text='<%# Eval("Status")%>' Visible="false" />
<asp:DropDownList ID="ddlFooterStatus" CssClass="changedropdown" runat="server" Width="120">
<img src="../../Themes/Images/reg-alert-icon.png" class="alert" id="imgstatus" style="margin-left: 5px;
float: right" alt="manditory" />
in .aspx.cs

in RowDataBound:
if (e.Row.RowType == DataControlRowType.DataRow)

DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus");
if (ddlType != null && ddlStatus != null)
ResponseDTO objResponseDTO3 = (ResponseDTO)new TP.Services().Execute(TVCConstants.GetstatusDetails);
ddlStatus.DataSource = objResponseDTO3.ReturnObject;
ddlStatus.DataTextField = "status";
ddlStatus.DataValueField = "status";
ddlStatus.SelectedItem.Text = lblNewStatus.Text;


Row Updating
GridViewRow row = (GridViewRow)grdCitationConfg.Rows[e.RowIndex];
DropDownList ddlstatus = (DropDownList)grdCitationConfg.Rows[e.RowIndex].Cells[1].Controls[1];
if (ddlstatus.SelectedValue.ToString() != "0")
obj.Status = ddlstatus.SelectedItem.Text;

thanks srinivas.

Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response