Dynamically bind dropdown in a gridview using entity framework:
Dynamically bind dropdown in a gridview using entity
framework:
Write
this code in aspx page:-
Grid.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Style="margin-top: 13px" OnRowDeleting="GridView1_RowDeleting" OnRowCommand="GridView1_RowCommand"
OnRowUpdated="GridView1_RowUpdated1” >
<Columns>
<asp:TemplateField HeaderText="Id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblId" Visible="false" runat="server"
Text='<%#Bind("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FName">
<ItemTemplate>
<asp:Label ID="lblFName" runat="server" Text='<%#Bind("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LName">
<ItemTemplate>
<asp:Label ID="lblLName" runat="server" Text='<%#Bind("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
// <asp:Label ID="lblCountry" runat="server" Text='<%#Bind("Country") %>' />
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%#Bind("City") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lblEdit" Text="Edit"
CommandName="ShowPopup" /></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lblView" Text="View"
CommandName="View" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
</asp:GridView>
Write
the below code in aspx.cs page
Grid.aspx.cs:-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var x = (from n in en.Employees
select new { n.Id, n.Country
}).ToList();
DropDownList dd1 = (DropDownList)e.Row.FindControl("DropDownList1");
dd1.DataSource = x;
dd1.DataTextField = "Country";
dd1.DataValueField = "Id";
dd1.DataBind();
}
}
Comments
Post a Comment