We use SQL datasource to bind GridView and DetailView.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give a suitable name gridview_demo.

Step 2: In Solution Explorer you will get your empty website, then add a Web Form and SQL Server Database. Here are the steps:

For Web Form

gridview_demo (Your Empty Website) - Right Click , Add New Item, then Web Form. Name it gridviewid_demo.aspx.

For SQL Server Database:

gridview_demo (Your Empty Website) - Right Click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.

Database chamber

Step 3: In Server Explorer, click on your database Database.mdf - Tables, Add New Table and form a table like this:

Table - tbl_data and don’t forget to make ID as IS Identity - True

design

Design chamber


Step 4: Now make some design for your application by going to gridviewid_demo.aspx and try the code like this:

Gridviewid_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1 {
  8. width: 229px;
  9. }
  10. .style2 {
  11. width: 24px;
  12. }
  13. .style3 {
  14. text-decoration: underline;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <form id="form1" runat="server">
  20. <div class="style3"> <strong>GridView to DetailsView<br />
  21. </strong> </div>
  22. <table style="width:100%;">
  23. <tr>
  24. <td class="style1">
  25. <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [id], [name] FROM [tbl_data]"></asp:SqlDataSource>
  26. </td>
  27. <td class="style2"> </td>
  28. <td> </td>
  29. </tr>
  30. <tr>
  31. <td class="style1">
  32. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource2" CellPadding="4" ForeColor="#333333" GridLines="None">
  33. <AlternatingRowStyle BackColor="White" />
  34. <Columns>
  35. <asp:BoundField DataField="id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="id" />
  36. <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
  37. <asp:CommandField ShowSelectButton="True" /> </Columns>
  38. <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  39. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  40. <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
  41. <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
  42. <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
  43. <SortedAscendingCellStyle BackColor="#FDF5AC" />
  44. <SortedAscendingHeaderStyle BackColor="#4D0000" />
  45. <SortedDescendingCellStyle BackColor="#FCF6C0" />
  46. <SortedDescendingHeaderStyle BackColor="#820000" /> </asp:GridView>
  47. </td>
  48. <td class="style2"> </td>
  49. <td> </td>
  50. </tr>
  51. <tr>
  52. <td class="style1">
  53. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [tbl_data] WHERE [id] = @id" InsertCommand="INSERT INTO [tbl_data] ([name], [age], [phone], [email], [city]) VALUES (@name, @age, @phone, @email, @city)" SelectCommand="SELECT [id], [name], [age], [phone], [email], [city] FROM [tbl_data] WHERE ([id] = @id)" UpdateCommand="UPDATE [tbl_data] SET [name] = @name, [age] = @age, [phone] = @phone, [email] = @email, [city] = @city WHERE [id] = @id">
  54. <DeleteParameters>
  55. <asp:Parameter Name="id" Type="Int32" /> </DeleteParameters>
  56. <InsertParameters>
  57. <asp:Parameter Name="name" Type="String" />
  58. <asp:Parameter Name="age" Type="Decimal" />
  59. <asp:Parameter Name="phone" Type="Decimal" />
  60. <asp:Parameter Name="email" Type="String" />
  61. <asp:Parameter Name="city" Type="String" /> </InsertParameters>
  62. <SelectParameters>
  63. <asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters>
  64. <UpdateParameters>
  65. <asp:Parameter Name="name" Type="String" />
  66. <asp:Parameter Name="age" Type="Decimal" />
  67. <asp:Parameter Name="phone" Type="Decimal" />
  68. <asp:Parameter Name="email" Type="String" />
  69. <asp:Parameter Name="city" Type="String" />
  70. <asp:Parameter Name="id" Type="Int32" /> </UpdateParameters>
  71. </asp:SqlDataSource>
  72. </td>
  73. <td class="style2"> </td>
  74. <td> </td>
  75. </tr>
  76. <tr>
  77. <td class="style1">
  78. <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="125px" CellPadding="4" ForeColor="#333333" GridLines="None">
  79. <AlternatingRowStyle BackColor="White" />
  80. <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
  81. <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
  82. <Fields>
  83. <asp:BoundField DataField="id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="id" />
  84. <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
  85. <asp:BoundField DataField="age" HeaderText="Age" SortExpression="age" />
  86. <asp:BoundField DataField="phone" HeaderText="Phone" SortExpression="phone" />
  87. <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
  88. <asp:BoundField DataField="city" HeaderText="City" SortExpression="city" /> </Fields>
  89. <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  90. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  91. <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
  92. <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> </asp:DetailsView>
  93. </td>
  94. <td class="style2"> </td>
  95. <td> </td>
  96. </tr>
  97. </table>
  98. </form>
  99. </body>
  100. </html>
Alternative method: [For GridView]

new data

Data Source Configuration Wizard Window

database

Configure Data Source

new connection

table

test query

Alternative method: [For DetailsView]

Alternative method

All other process for DetailsView is same as that of above GridView. Just make sure to change these settings.

settings

detailsview

Finish this whole process; you will see your two SQL DataSource, one for GridView and other for DetailsView.

Output chamber

gridview result

Output

Hope you like it. Thank you for reading.