Anu

Anu

  • 1.5k
  • 153
  • 6.4k

dropdownlist in gridview

Aug 3 2023 1:13 PM

Hi,

I am using a dropdownlist inside a datagrid in my asp.net application. When the user cick on the edit button in a datagrid, I want to show the previously saved item  as selected in a dropdownlist. I am using the below code to select item in the dropdownlist. Its working file if the items bind to the dropdownlist is same and in the same case when comparing with the previously saved item. But its failing when the case is not matching. Can some one please check the below code and help to select the item in a dropdownlist without checking the case of items.

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {

            DataRowView drv = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                    DropDownList ddltemptable = (DropDownList)e.Row.FindControl("ddltemptablename");
                    DataTable dttemptable = load_temptable();
                    ddltemptable.DataSource = dttemptable;
                    ddltemptable.DataTextField = "table_name";
                    ddltemptable.DataValueField = "table_name";
                    ddltemptable.DataBind();
                    string selectedtemptable = DataBinder.Eval(e.Row.DataItem, "TempTableName").ToString();
                    ddltemptable.Items.Insert(0, new ListItem("-Select-", "0"));
                    if (selectedtemptable != "")
                        ddltemptable.Items.FindByText(selectedtemptable).Selected = true;
                    else
                    {
                        ddltemptable.ClearSelection();
                        ddltemptable.Items.FindByText("-Select-").Selected = true;
                    }

}

}

 


Answers (4)