Sunday, September 13, 2009

Raising DropDownList SelectedIndexChanged From GridView Control

In this post I will show you how you can use the DropDownList SelectedIndexChanged, when DropDownList is placed inside the grid view control. The main form consist of grid view control. The grid view control consist of order name, current status label which will be update when use select one of the value from the dropdownList control in the grid view control.

Here is the html code which is used to bind the SelectedIndexChanged of the dropdownList with the code behind event handler. And also notice that Autopost back property of the DropDownList is set to true. The DropDownList control is bind to the data source on the grdvCategoryList_RowDataBound event of the grid view control.
<asp:TemplateField HeaderText="Order Status">
<ItemTemplate>
<asp:DropDownList ID="ddlDropDownList" runat="server" CssClass="inputfields" OnSelectedIndexChanged="ddlDropDownList_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
And Here is the code which is placed in the selectedIndexChanged of the dropDownList control. You can see in this code that fist I have converted the sender to the dropDownList control and then by using that DropwDownList control I have get the row of the grid where the current grid view control is placed. Now You have found the row of the DropDownList control no you can find the other control in that row. Here I have find the label control which will display the current value from the DropDownList.
DropDownList ddlCurrentDropDownList = (DropDownList)sender;
GridViewRow grdrDropDownRow = ((GridViewRow)ddlCurrentDropDownList.Parent.Parent);
Label lblCurrentStatus = (Label)grdrDropDownRow.FindControl("lblOrderStatus");
if (lblCurrentStatus != null)
lblCurrentStatus.Text = ddlCurrentDropDownList.SelectedItem.Text;
You can download the source code from here
All and any comments / bugs / suggestions are welcomed!

15 comments:

fzshah76 said...

was facing a problem In edit mode of grid view could not bind back the data to label, this method worked perfectly.


Awesome job

Thank You

jeff said...

Thanks!

Very clear and simple and just what I needed to solve a problem I was having today.

Anonymous said...

Very Nice and Good Example....

Anonymous said...

Thanks a lot man!!!! :D this helped me a lot!!

Anonymous said...

thanks a lot yar..u helped me a lot..i was search for his from 2 days...

Anonymous said...

great help. Thanks.

Anonymous said...

Thank you very much, this worked very good for my issue. Please keep on posting.

Anonymous said...

What about, if the same gridview is in updatepanel of ajax?

Akshit Zaveri said...

Simple, stable & fast.. Thanks buddy..

Rakesh Kumar TUTU said...

Very good example of update grid view column on selected index change...

Anonymous said...

How to do change the dropdownlist's index, inside gridview using JavaScript?

i_harshit@rediffmail.com

lakshmiindhu said...

Hi,Its fine But when we are taking static selected items from dropdown selectindexchange event happen display modalpopup .

I m new to Dotnet

Marc-André said...

I will like something similar, but with the dropdown list in edititemtemplate, not in itemtemplate.

Anonymous said...

Thanks a bunch. That really helped!!!

Anonymous said...

Thanks.. Its help me lot.

Have a Good work boss