Articles → ASP .NET GRIDVIEW → Bind dropdown inside gridview in asp.net

Bind dropdown inside gridview in asp.net






Prerequisites



























Steps Involved





















Add tables in database





Click to Enlarge


Click to Enlarge


Click to Enlarge


Click to Enlarge


Create a new web application and add ‘GridView’ control

in the ‘Default.aspx’ page




<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false"></asp:GridView>



Add connection string for database in web.config




<connectionStrings>
	<add name="MyConnectionString" connectionString="conn_string" providerName="System.Data.SqlClient"/>
</connectionStrings>



Add SQL data source for binding gridview




<asp:SqlDataSource ID="sdsGrid" runat="server"
  ConnectionString="
	<%$ ConnectionStrings:MyConnectionString %>"
  SelectCommand="select C.ClassId, C.ClassDisplayName FROM Class C"  />



Bind grid with SQL data source




<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false" 
        DataSourceID="sdsGrid">
	<Columns>
		<asp:TemplateField HeaderText="Class">
			<ItemTemplate>
				<asp:Label ID="lblClassName" runat="server" Text='<%#Eval("ClassDisplayName") %>'>
				</asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Teacher">
			<ItemTemplate>
				<asp:DropDownList ID="ddlTeacher" runat="server"></asp:DropDownList>
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsGrid" runat="server"
  ConnectionString="
	<%$ ConnectionStrings:MyConnectionString %>"
  SelectCommand="select C.ClassId, C.ClassDisplayName FROM Class C"  />






<asp:SqlDataSource ID="sdsTeacher" runat="server"
  ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
  SelectCommand="SELECT ([TeacherFirstName] + ' ' + [TeacherLastName]) as TeacherName  FROM [TeacherDetails]">
</asp:SqlDataSource>



Bind dropdown with SQL data source




<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false" 
        DataSourceID="sdsGrid">
	<Columns>
		<asp:TemplateField HeaderText="Class">
			<ItemTemplate>
				<asp:Label ID="lblClassName" runat="server" Text='<%#Eval("ClassDisplayName") %>'>
				</asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Teacher">
			<ItemTemplate>
				<asp:DropDownList ID="ddlTeacher" runat="server" DataSourceID="sdsTeacher" DataTextField="TeacherName"></asp:DropDownList>
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsGrid" runat="server"
  ConnectionString="
	<%$ ConnectionStrings:MyConnectionString %>"
  SelectCommand="select C.ClassId, C.ClassDisplayName FROM Class C"  />
	<asp:SqlDataSource ID="sdsTeacher" runat="server"
  ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
  SelectCommand="SELECT ([TeacherFirstName] + ' ' + [TeacherLastName]) as TeacherName  FROM [TeacherDetails]">
	</asp:SqlDataSource>



Output





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, April 30, 2014

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250