Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvlevleone" runat="server" AllowSorting="true" AutoGenerateColumns="false" CssClass="active right"
BorderColor="#FFFF99" GridLines="Horizontal" ShowHeaderWhenEmpty="True" OnRowDataBound="gvlevleone_RowDataBound"
ShowFooter="True" OnRowCreated="gvlevleone_RowCreated">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" />
<Columns>
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:TextBox ID="txtcode" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Head">
<ItemTemplate>
<asp:Label ID="lbfour" runat="server" Visible="false" />
<asp:DropDownList runat="server" ToolTip="Select" ID="ddlfour" Width="200px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
SqlConnection con = new SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=WHMS;MultipleActiveResultSets=True;");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("ddlfour", typeof(int)) });
dt.Rows.Add();
this.gvlevleone.DataSource = dt;
this.gvlevleone.DataBind();
}
}
protected void gvlevleone_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlContnam = (DropDownList)e.Row.FindControl("ddlfour");
SqlCommand cmd = new SqlCommand("select * from Customers ", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
ddlContnam.DataSource = dt;
ddlContnam.DataTextField = "Name";
ddlContnam.DataValueField = "CustomerId";
ddlContnam.DataBind();
ddlContnam.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}
}
protected void gvlevleone_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.EmptyDataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlfour");
if (ddl != null)
{
ddl.Items.Add(new ListItem("Select", "0"));
}
}
}
VB.Net
Private con As SqlConnection = New SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=WHMS;MultipleActiveResultSets=True;")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ddlfour", GetType(Integer))})
dt.Rows.Add()
Me.gvlevleone.DataSource = dt
Me.gvlevleone.DataBind()
End If
End Sub
Protected Sub gvlevleone_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlContnam As DropDownList = CType(e.Row.FindControl("ddlfour"), DropDownList)
Dim cmd As SqlCommand = New SqlCommand("select * from Customers ", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
ddlContnam.DataSource = dt
ddlContnam.DataTextField = "Name"
ddlContnam.DataValueField = "CustomerId"
ddlContnam.DataBind()
ddlContnam.Items.Insert(0, New ListItem("--Select Customer--", "0"))
End If
End Sub
Protected Sub gvlevleone_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.EmptyDataRow Then
Dim ddl As DropDownList = CType(e.Row.FindControl("ddlfour"), DropDownList)
If ddl IsNot Nothing Then
ddl.Items.Add(New ListItem("Select", "0"))
End If
End If
End Sub