Question

I have drop-down that is in a gridview and it is binding but the problem is that is binding only the first row and keep binding the same value the rest of the gridview. I am expecting a drop down for each each row in the gridview and the content of the drop down will be different based on ID in the gridvew. In one row it might be Yes, No and the other one might be Yes, No, NA. Here is my code:

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddl_Answer;
            foreach (GridViewRow grdRow in GridView1.Rows)
            {

                //get current index selected
                int current_eng_sk = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
                ddl_Answer = (DropDownList)(GridView1.Rows[grdRow.RowIndex].Cells[2].FindControl("ddl_Answer"));
                String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString;
                SqlConnection con2 = new SqlConnection(strConnString);
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlCommand cmd1 = new SqlCommand("select distinct DD_ANSWER from table1 D, table2 Q where Q.ANS_OPT = D.ID and  ENG_SK= '" + current_eng_sk + "' and Q.ANS_OPT is not null ");

                cmd1.Connection = con2;
                con2.Open();
                ddl_Answer.DataSource = cmd1.ExecuteReader();
                ddl_Answer.DataTextField = "DD_ANSWER";
                ddl_Answer.DataValueField = "DD_ANSWER";
                ddl_Answer.DataBind();
                con2.Close();

            }
}
Was it helpful?

Solution

You are already in the RowDataBound event for the GridView. This is fired once per row so you don't need to then further foreach (GridViewRow grdRow in GridView1.Rows) each time through. You should be able to simplify your code down to:

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl_Answer;
        //get current index selected
        int current_eng_sk = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
        ddl_Answer = e.Row.FindControl("ddl_Answer") as DropDownList;
        using (SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString)) 
        {
            con2.Open();
            using (SqlCommand cmd1 = new SqlCommand("select distinct DD_ANSWER from table1 D, table2 Q where Q.ANS_OPT = D.ID and  ENG_SK= '" + current_eng_sk + "' and Q.ANS_OPT is not null ", con2)) 
            {
                ddl_Answer.DataSource = cmd1.ExecuteReader();
                ddl_Answer.DataTextField = "DD_ANSWER";
                ddl_Answer.DataValueField = "DD_ANSWER";
                ddl_Answer.DataBind();
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top