Articles → ASP .NET GRIDVIEW → Bind Dropdown Inside Gridview In Asp.Net

Bind Dropdown Inside Gridview In Asp.Net






Prerequisites










  1. How to create a table in SQL server?
  2. How to insert data in a table (manually or through insert script)
  3. How to create website using visual studio?
  4. What is a gridview control?
  5. How to add controls inside gridview?
  6. How to define connection string inside web.config?
  7. What is SQL data source control?
  8. How to bind grid and dropdown using SQL data source control?

Steps Involved




  1. Add tables in database
  2. Create a new web application and add ‘GridView’ control in the ‘Default.aspx’ page
  3. Add connection string for database in web.config
  4. Add SQL data source for binding gridview
  5. Bind grid with SQL data source
  6. Add sql data source for teacher drop down
  7. Bind dropdown with SQL data source
  8. Output

Add Tables In Database




Picture showing the table schema of the class table
Click to Enlarge

Picture showing the records in the class table
Click to Enlarge

Picture showing the table schema of the Teacher table
Click to Enlarge

Picture showing the records in the Teacher table
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




Picture showing the datagrid with dropdown bound with records
Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250