Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

How to bind dropdownlist in gridview rowdatabound in c#?

This is my Drop Down List in Edit Item Template on GridView in c#

<asp:DropDownList runat="server" ID="ddlFl" AutoPostBack="true"
 OnSelectedIndexChanged="ddlFl_SelectedIndexChanged"
      DataValueField='<%# Eval("ID").ToString() %>'>
   <asp:ListItem Text="[ Select ]" Value=""></asp:ListItem>
   <asp:ListItem Text="-------------" Value=""></asp:ListItem>
   <asp:ListItem Text="0" Value="0"></asp:ListItem>
   <asp:ListItem Text="1" Value="1"></asp:ListItem>
   <asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>

When user clicks on edit, grid enters into edit mode.

In case of Product Sub Category, we load the list of all product categories available in the tbl_2 table into a drop down list and are available for user selection and if on the row exist value this is selected in a drop down list.

I have tried in this mode without success because I have error:

Data binding methods such as Eval (), XPath (), and Bind () can only be used in the context of a data-bound control.

How to do resolve this?

Can you help me?

Thank you in advance for any help, really appreciated.

protected void ddlFl_SelectedIndexChanged(object sender, EventArgs e)
{

    DropDownList btn = (DropDownList)sender;
    string indexID = btn.DataValueField;
    string LinkID = btn.SelectedValue;
    GridViewRow row = (GridViewRow)btn.NamingContainer;

    Response.Write(LinkID);

}


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        if (e.Row.DataItem != null)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlFl = (DropDownList)e.Row.FindControl("ddlFl");
                ddlFl.DataTextField = "Dip";
                ddlFl.DataValueField = "Dip";
                ddlFl.DataSource = RetrieveSubCategories();
                ddlFl.DataBind();
                DataRowView dr = e.Row.DataItem as DataRowView;
                ddlFl.SelectedValue = dr["Dip"].ToString();
            }
        }
    }
}


private DataTable RetrieveSubCategories()
{
    DataTable dtSubCategories = new DataTable();

    sql = @String.Format(" SELECT Dip FROM `tbl_2` GROUP BY Dip; ");

    using (OdbcConnection myConnectionString =
      new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
    {
        using (OdbcCommand cmd = 
            new OdbcCommand(sql, myConnectionString))
        {
            {
                OdbcDataAdapter adapter = 
                    new OdbcDataAdapter(cmd);
                adapter.Fill(dtSubCategories);
            }
        }
    }
    return dtSubCategories;
}

Upvotes: 1

Views: 2659

Answers (1)

user1773603
user1773603

Reputation:

DataSource must be at first position and then Data Text and Value Fields:

DropDownList ddlFl = (DropDownList)e.Row.FindControl("ddlFl");
ddlFl.DataSource = RetrieveSubCategories();
ddlFl.DataTextField = "Dip";
ddlFl.DataValueField = "Dip";
ddlFl.DataBind();

Upvotes: 1

Related Questions