Search This Blog

Wednesday, August 25, 2010

GridView with DropDownList In all Type of Template Fields

Most of the coders have problem when they are binding a gridview with a dropdownlist within the gridview. And some problem may occur in the edit mode with the selected vlaue of the dropdownlist to solve the problem you can use like the following

In the GridView markup need to code like this:

<asp:TemplateField HeaderText="Event">
<EditItemTemplate>
<asp:DropDownList ID="ddlGvEditEvents" runat="server" AutoPostBack="True" DataSource='<%#getvalues() %>'
DataTextField="EventName" DataValueField="EventID" Font-Size="Small" Width="150px" SelectedValue='<%# Bind("EventId") %>'>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlGvFooterEvents" runat="server" AutoPostBack="True" Font-Size="Small" DataSource='<%#getvalues() %>' DataTextField="EventName" DataValueField="EventID" Width="150px">
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlGvItemEvents" runat="server" AutoPostBack="True" Font-Size="Small" DataSource='<%#getvalues() %>' DataTextField="EventName" DataValueField="EventID" Width="150px" SelectedValue='<%# Bind("EventId") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
 
C# Code Behind:
 
public DataSet getvalues()
{
try
   {
    DataSet dsEvents = new DataSet();
    //Your method to fill the dataset from the DataBase
    ViewState["EventTable"] = dsEvents;
    return dsEvents;
    }
catch (Exception ex)
{
  throw ex;
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.