How to set SelectedValue of DropDownList in GridView EditTemplate

64,426

Solution 1

DataValueField seems to be wrong - shouldn't it be DepartmentId? Similarly, you need to have SelectedValue='<%# Eval("**DepartmentId**") %>' - DepartmentName would be the SeletectText.

Solution 2

The use of the GridView_DataBound event handler solves the problem.

In your case you will need to add a HiddenField to store the PK_DepartmentId value:

<asp:GridView ID="gvExample" runat="server" AutoGenerateColumns="False" OnDataBound="gvExample_DataBound">
  <Columns>
    <asp:TemplateField HeaderText="Department">
      <EditItemTemplate>
        <asp:DropDownList ID="ddlDepartment_Edit" runat="server" DataSourceID="dsDepartment_Edit"
          DataTextField="DepartmentName" DataValueField="PK_DepartmentId">
        </asp:DropDownList>
        <asp:HiddenField ID="hfDepartmentId" runat="server" Value='<%# Bind("PK_DepartmentId") %>' />
        <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
          ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>'>
        </asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" ButtonType="Button" />
  </Columns>
</asp:GridView>

protected void gvExample_DataBound(object sender, EventArgs e)
{
  foreach (GridViewRow gvRow in gvExample.Rows)
  {
    DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList;
    HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField;

    if (ddlDepartment != null && hfDepartmentId != null)
    {
      ddlDepartment.SelectedValue = hfDepartmentId.Value;
    }
  }
}

Solution 3

Why are you guys suggesting to use loops, when there is a GridView method specifically made for when a row's condition changes - the RowDataBound()?

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvRow = (GridViewRow)e.Row;
            HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID");
            if (hfAgentID != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
                    ddlAgent.SelectedValue = hfAgentID.Value;
                }
            }
        }
Share:
64,426
Shantanu Gupta
Author by

Shantanu Gupta

Debugging Minds... Looking For Learning Opportunities "Opportunities are Often The Beginning of Great Enterprise..." LinkedIn: https://www.linkedin.com/in/shantanufrom4387/

Updated on September 13, 2020

Comments

  • Shantanu Gupta
    Shantanu Gupta over 3 years

    I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.

    I tried to use AppendDataBoundItems=true but it is still not working. I also want to set the its default value to the value that was being displayed in label of itemtemplate i.e. DropDownList's SelectedValue='<%# Eval("DepartmentName") %>' but thie property is not available to me in dropdownlist. What could be the reason. ??

    <EditItemTemplate>
        <asp:DropDownList ID="ddlDepartment_Edit" runat="server" 
            DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName" 
            DataValueField="PK_DepartmentId">
        </asp:DropDownList>
        <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" 
            ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"  
            ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" 
            SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>                                 
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
        </asp:Label>
    </ItemTemplate>   
    

    I am using GridView

  • Shantanu Gupta
    Shantanu Gupta almost 14 years
    this is where i am in trouble. No such is property is getting visible to me i.e. SelectedValue or SelectedText, so that I can assign some value to them. About DataValueField, yes you are right it should be primary key. But not a part of discussion until these two properties gets available to me.
  • VinayC
    VinayC almost 14 years
    VS Designer may not be showing you the property in intelli-sense but its there. There are two properties - SelectedValue and SelectedIndex. What happens if you write SelectedIndex='<%# Eval('DepartmentId') %>'?
  • VinayC
    VinayC almost 14 years
    My mistake - it should have been as SelectedValue = '<%# Eval('DepartmentId') %>'.