c# - Progmatically binding the dropdownlist in edititem template in gridview -


i trying bind dropdownlist in gridview under edititemtemplate under rowbound event. gives me blank rows in drop down list:here design

<asp:templatefield headertext="description">    <itemtemplate>       <asp:label id="lbldescription" runat="server" text='<%# bind("description") %>' width="400px"></asp:label>    </itemtemplate>    <edititemtemplate>       <asp:label id="lblcity" runat="server" text='<%# bind("description") %>'></asp:label>       <asp:dropdownlist id="ddlcities" runat="server" width="400px"></asp:dropdownlist>    </edititemtemplate>    <footertemplate>       <asp:dropdownlist id="ddlnewdescfooter" runat="server" width="400px" onselectedindexchanged="ddlcitiesfooter_selectedindexchanged">       </asp:dropdownlist>    </footertemplate> </asp:templatefield> 

now here row databound event:

protected void grdferries_rowdatabound(object sender, gridviewroweventargs e) {    if (e.row.rowstate == datacontrolrowstate.edit)    {        dropdownlist ddlcities = (dropdownlist)e.row.findcontrol("ddlcities");        dataaccessclass dac = new dataaccessclass();        string query = "select description joursferies year(joursferies.date) >= year(getdate()) order date asc";        datatable dtddl = dac.returndatatablefromquery(query, dbconnectionstring);        ddlcities.datasource = dtddl;        ddlcities.datatextfield = "description";        ddlcities.datavaluefield = "description";        ddlcities.databind();        ddlcities.items.insert(0, new listitem("--select--", "0"));    } } 

i getting blank dropdownlist. pls help.

that's because block of code have written never executed. need check if row datarow , not state. have check whether row being bounded in edit mode or not. should work you:-

if (e.row.rowtype == datacontrolrowtype.datarow && grdferries.editindex == e.row.rowindex) {    dropdownlist ddlcities = (dropdownlist)e.row.findcontrol("ddlcities");    dataaccessclass dac = new dataaccessclass();    ..and on } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -