I am writing this article on demand as I got a request from one of my friends. He told me that he is looking for this requirement so I created this. As per his requirement, he needs to show data from SQL Server database table in ASP.NET GridView using jQuery, JSON and AJAX call.

Below is my Data Table from which I am showing data.

table

Script of my Table

  1. CREATE TABLE [dbo].[Customers](
  2. [CustomerID] [int] IDENTITY(1,1)NOTNULL,
  3. [Name] [varchar](50)NULL,
  4. [Mobile] [varchar](50)NULL,
  5. [City] [varchar](50)NULL,
  6. CONSTRAINT [PK_Customer] PRIMARYKEYCLUSTERED
  7. (
  8. [CustomerID] ASC
  9. )WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,
  10. IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,
  11. ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
  12. )ON [PRIMARY]
  13. GO
Data in my Table

table

Now right click on Project’s Solution Explorer, then click Manage Nuget

Type jQuery and Install

install

Below is my aspx code
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm2.aspx.cs"Inherits="ExpandFillNestedGridView.WebForm2"%>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Showing Data in ASP.NET Grid View Using jQuery, JSON & AJAX Call</title>
  6. <script src="Scripts/jquery-2.2.0.min.js">
  7. </script>
  8. <script type="text/javascript">
  9. $(document).ready(function ()
  10. {
  11. $("#btnShowData").click(function ()
  12. {
  13. $.ajax
  14. ({
  15. type: "POST", contentType: "application/json; charset=utf-8", url: "WebForm2.aspx/BindCustomers", data: "{}",
  16. dataType: "json", success: function (result) { for (vari = 0; i<result.d.length; i++)
  17. {
  18. $( "#gvData").append( "<tr><td>" + result.d[i].CustomerID + "</td><td>" + result.d[i].Name + "</td><td>" + result.d[i].Mobile + "</td><td>" + result.d[i].City + "</td></tr>");
  19. }
  20. }, error: function (result)
  21. {
  22. alert( "Error");
  23. }
  24. });
  25. });
  26. });
  27. </script>
  28. </head>
  29. <body>
  30. <tablestyle="background-color: yellow; border: solid 5px red; width: 100%" align="center">
  31. <tr>
  32. <tdstyle="background-color: orangered; padding: 2px; text-align: center; color: white; font-weight: bold; font-size: 14pt;">Showing Data Using jQuery, JSON & AJAX Call</td>
  33. </tr>
  34. <tr>
  35. <td>
  36. <buttonid="btnShowData" runat="server">Get Data</button>
  37. <br/>
  38. <br/>
  39. <formid="form1" runat="server" style="background-color:deepskyblue; padding:5px;">
  40. <asp:GridViewID="gvData" runat="server" CellPadding="4" ShowHeaderWhenEmpty="true" ForeColor="White" Width="100%">
  41. <HeaderStyleBackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  42. <RowStyleBackColor="#EFF3FB" />
  43. </asp:GridView>
  44. </form>
  45. </td>
  46. </tr>
  47. </table>
  48. </body>
  49. </html>
Below is my aspx.cs code
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Web.Services;
  5. using System.Data.SqlClient;
  6. using System.Collections.Generic;
  7. namespace ExpandFillNestedGridView
  8. {
  9. public partial class WebForm2: System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack)
  14. {
  15. BindDummyGridrow();
  16. }
  17. }
  18. public void BindDummyGridrow()
  19. {
  20. DataTable dt = new DataTable();
  21. dt.Columns.Add("Customer ID");
  22. dt.Columns.Add("Name");
  23. dt.Columns.Add("Mobile");
  24. dt.Columns.Add("City");
  25. gvData.DataSource = dt;
  26. gvData.DataBind();
  27. }
  28. [WebMethod]
  29. public static Customer[] BindCustomers()
  30. {
  31. string connectionString = @ "Data Source=.; database=CompanyDB;Integrated Security=true";
  32. DataTabledt = newDataTable();
  33. List < Customer > custList = newList < Customer > ();
  34. using(SqlConnection con = newSqlConnection(connectionString))
  35. {
  36. using(SqlCommand command = newSqlCommand("select * from Customers", con))
  37. {
  38. con.Open();
  39. SqlDataAdapter da = newSqlDataAdapter(command);
  40. da.Fill(dt);
  41. foreach(DataRowdtrowindt.Rows)
  42. {
  43. Customercust = newCustomer();
  44. cust.CustomerID = dtrow["CustomerID"].ToString();
  45. cust.Name = dtrow["Name"].ToString();
  46. cust.Mobile = dtrow["Mobile"].ToString();
  47. cust.City = dtrow["City"].ToString();
  48. custList.Add(cust);
  49. }
  50. }
  51. }
  52. return custList.ToArray();
  53. }
  54. public class Customer
  55. {
  56. public string CustomerID
  57. {
  58. get;
  59. set;
  60. }
  61. public string Name
  62. {
  63. get;
  64. set;
  65. }
  66. public string Mobile
  67. {
  68. get;
  69. set;
  70. }
  71. public string City
  72. {
  73. get;
  74. set;
  75. }
  76. }
  77. }
  78. }
Now run your application,

run

run