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.
You can download the source code from hereDropDownList ddlCurrentDropDownList = (DropDownList)sender;
GridViewRow grdrDropDownRow = ((GridViewRow)ddlCurrentDropDownList.Parent.Parent);
Label lblCurrentStatus = (Label)grdrDropDownRow.FindControl("lblOrderStatus");
if (lblCurrentStatus != null)
lblCurrentStatus.Text = ddlCurrentDropDownList.SelectedItem.Text;
All and any comments / bugs / suggestions are welcomed!
19 comments:
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
Thanks!
Very clear and simple and just what I needed to solve a problem I was having today.
Very Nice and Good Example....
Thanks a lot man!!!! :D this helped me a lot!!
thanks a lot yar..u helped me a lot..i was search for his from 2 days...
great help. Thanks.
Thank you very much, this worked very good for my issue. Please keep on posting.
What about, if the same gridview is in updatepanel of ajax?
Simple, stable & fast.. Thanks buddy..
Very good example of update grid view column on selected index change...
How to do change the dropdownlist's index, inside gridview using JavaScript?
i_harshit@rediffmail.com
Hi,Its fine But when we are taking static selected items from dropdown selectindexchange event happen display modalpopup .
I m new to Dotnet
I will like something similar, but with the dropdown list in edititemtemplate, not in itemtemplate.
Thanks a bunch. That really helped!!!
Thanks.. Its help me lot.
Have a Good work boss
Great Post!! saving my life
Thanks its Helpful
Thank you for this article. You just saved me after hours of work on this gridview dropdown update to db.
Post a Comment