Articles → ASP .NET GRIDVIEW → Bind Dropdown Inside Gridview In Asp.Net
Bind Dropdown Inside Gridview In Asp.Net
Prerequisites
- How to create a table in SQL server?
- How to insert data in a table (manually or through insert script)
- How to create website using visual studio?
- What is a gridview control?
- How to add controls inside gridview?
- How to define connection string inside web.config?
- What is SQL data source control?
- How to bind grid and dropdown using SQL data source control?
Steps Involved
- Add tables in database
- Create a new web application and add ‘GridView’ control in the ‘Default.aspx’ page
- Add connection string for database in web.config
- Add SQL data source for binding gridview
- Bind grid with SQL data source
- Add sql data source for teacher drop down
- Bind dropdown with SQL data source
- Output
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