Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam not able to populate the dropdownlist in edit item template with the data before data binding.because of this on databind() iam getting the error.

aspx code:

<asp:TemplateField HeaderText="City">
                       <ItemTemplate>
                           <asp:Label Text='<%# Eval("City") %>' runat="server" />
                       </ItemTemplate>
                       <EditItemTemplate>
                          <asp:TextBox ID="txtCity" Text='<%# Eval("City") %>' runat="server" />
                       </EditItemTemplate>
                       <FooterTemplate>
                           <asp:DropDownList ID="CityDropDownListFooter"  runat="server" onselectedindexchanged="cityDropDownList_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
                       </FooterTemplate>
                   </asp:TemplateField>



aspx.cs code:

if (dtbl.Rows.Count > 0)
{
organizationGridview.DataSource = dtbl;
organizationGridview.DataBind();
}

What I have tried:

aspx code:

<pre> <asp:TemplateField HeaderText="City">
                        <ItemTemplate>
                            <asp:Label Text='<%# Eval("City") %>' runat="server" />
                        </ItemTemplate>
                        <EditItemTemplate>
                           <asp:TextBox ID="txtCity" Text='<%# Eval("City") %>' runat="server" />
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="CityDropDownListFooter"  runat="server" onselectedindexchanged="cityDropDownList_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>



aspx.cs code:

if (dtbl.Rows.Count > 0)
{
organizationGridview.DataSource = dtbl;
organizationGridview.DataBind();
}

how do i get the data in the edit item dropdownlist?

surprisingly, rowdatabound function is not triggered on databind() call.

protected void organizationGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
Control ctrl = e.Row.FindControl("CityDropDownList");
if(ctrl!=null)
{
DropDownList ddl = (DropDownList)ctrl;
string connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["mydatabase"].ConnectionString;

MySqlConnection mcon = new MySqlConnection(connectionString);
mcon.Open();
String cmdText = "select distinct City from Organization union all select 'other';";

MySqlCommand cmd = new MySqlCommand(cmdText, mcon);
MySqlDataAdapter mysqladapter = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
mysqladapter.Fill(ds);
ddl.DataTextField = ds.Tables[0].Columns["City"].ToString();
ddl.DataSource = ds.Tables[0];
ddl.DataBind();
mcon.Close();
}
}
Posted
Updated 7-Jan-20 6:21am

1 solution

The RowDataBound event should work. You need to check the RowType first and then check for the RowState. For example:

C#
protected void organizationGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList ddlCity = (DropDownList)e.Row.FindControl("CityDropDownListFooter");
            
            //Do something with ddlCity here   

        }
    }
}


Since your DropDownList resides within a FooterTemplate, then you need check the RowType to ensure that you are only manipulating the rows of type Footer. Please note that a GridView is composed of several row types such as Header, DataRow, EmptyDataRow, Footer, Pager and Separator. The next line in code above is the critical part of the code and that is to determine the Edit state.

Accessing the controls from within <edititemtemplate> is a bit tricky, especially if you are not really familiar with how the stuff works within a GridView. Equating the RowState to DataControlState.Edit isn't really accurate and you might get an exception when doing so. The RowState property is a bitwise combination. Therefore, the RowState could indicate that you are in the Edit state and the Alternate state. Hence, you cannot do a simple equality check when you are in Edit mode. Instead you must do something like this when accessing controls within EditItemTemplate.

C#
if ((e.Row.RowState & DataControlRowState.Edit) > 0)   
{  
     //do your stuff here  
} 


We use the bitwise "&" operator to determine if the GridView is in Edit mode and check the result if its greater than zero. For details about the Bitwise operator see: Bitwise Operators in C#.[^]
 
Share this answer
 
v3
Comments
kkgj 7-Jan-20 14:08pm    
Thanks for your help,i appreciate it.

i need dropdownlist in the edit item template not in the footer template.footer dropdownlist works fine.

aspx code:

OnRowCommand="organizationGridview_RowCommand" OnRowEditing="organizationGridview_RowEditing" OnRowDataBound="organizationGridview_RowDataBound" OnRowCancelingEdit="organizationGridview_RowCancelingEdit"
OnRowUpdating="organizationGridview_RowUpdating" OnRowDeleting="organizationGridview_RowDeleting"

<asp:templatefield headertext="City">
<itemtemplate>
<asp:label text="<%# Eval("City") %>" runat="server">

<edititemtemplate>
<asp:dropdownlist id="CityDropDownList" text="<%# Eval("City") %>" runat="server" autopostback="True">

<footertemplate>
<asp:dropdownlist id="CityDropDownListFooter" runat="server" onselectedindexchanged="cityDropDownList_SelectedIndexChanged" autopostback="True">


but some show its not hitting the rowdatabound function to add items to the citydropdownlist at edit item template. am i missing something here?


if (dtbl.Rows.Count > 0)
{
organizationGridview.DataSource = dtbl;
organizationGridview.DataBind();
}

on databind() call i got this below error message
"''CityDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items."
Vincent Maverick Durano 7-Jan-20 14:25pm    
Then just changed the RowType checking to DataRow. I've updated the solution for your reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900