Hi Rezu2215,
Refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvCustomers_RowDataBound">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TestEntities test = new TestEntities();
var query = (from c in test.Customers select c);
gvCustomers.DataSource = query;
gvCustomers.DataBind();
}
}
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TestEntities test = new TestEntities();
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
var query = (from c in test.Customers
orderby c.CustomerId
select new
{
c.CustomerId,
c.Country
}).ToList();
ddlCountry.DataSource = query;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "CustomerId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select Country--", ""));
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim test As TestEntities = New TestEntities()
Dim query = (From c In test.Customers Select c)
gvCustomers.DataSource = query
gvCustomers.DataBind()
End If
End Sub
Protected Sub gvCustomers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim test As TestEntities = New TestEntities()
Dim ddlCountry As DropDownList = CType(e.Row.FindControl("ddlCountry"), DropDownList)
Dim query = (From c In test.Customers Order By c.CustomerId Select New With {c.CustomerId, c.Country
}).ToList()
ddlCountry.DataSource = query
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "CustomerId"
ddlCountry.DataBind()
ddlCountry.Items.Insert(0, New ListItem("--Select Country--", ""))
End If
End Sub
Screenshot
