Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set SelectedValue of DropDownList in GridView EditTemplate

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

like image 971
Shantanu Gupta Avatar asked Aug 13 '10 07:08

Shantanu Gupta


People also ask

How do you show records in a GridView using DropDownList in web application?

Create Database Now, take an ASP.NET web application -> Take one DropDownList control and one GridView control. Your form will look as in the following figure. Now set the AutoPostBack property of the DropDownList to "True".

What is EditItemTemplate in GridView in ASP NET?

Add an EditItemTemplate in the TemplateField that specifies a custom user interface (UI) for the item in edit mode. Set the Command name property to Edit in the Edit button, Update in the Update button and Cancel in the Cancel Button depending on their respective Events.


2 Answers

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

like image 55
VinayC Avatar answered Oct 10 '22 20:10

VinayC


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;
    }
  }
}
like image 45
SHS Avatar answered Oct 10 '22 20:10

SHS