In this article I will explain how to bind Dataset to ASP.Net GridView control using jQuery AJAX and Web Methods.
Database and Connection string
For this sample to work you will need to download the Microsoft Northwind database using the following link
Below is the connection string from the Web.Config file
<add name="conString" connectionString="Data Source=.\SQLExpress;
database=Northwind;Integrated Security=true"/>
GridView Markup
The GridView markup is very simple with three bound fields.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
    Font-Size="10pt" RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor = "White">
        <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="ContactName" />
        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
You will have to inherit the following namespaces in order to use this sample
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
Binding the Dummy row
You will need to bind the one dummy row to the ASP.Net GridView so that the ASP.Net GridView is rendered. Also the dummy row is required to retain the CSS styles and formatting of the row.
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
private void BindDummyRow()
    DataTable dummy = new DataTable();
    gvCustomers.DataSource = dummy;

Above you will see I have created a dummy DataTable with three columns that I need to bind to the ASP.Net GridView.
WebMethod to handle jQuery AJAX
Now we will create a Web method that will actually handle the AJAX requests from the jQuery AJAX handler and return the records fetched from the Customers table of the Northwind database as a string of XML.
public static string GetCustomers()
    string query = "SELECT top 10 CustomerID, ContactName, City FROM Customers";
    SqlCommand cmd = new SqlCommand(query);
    return GetData(cmd).GetXml();
private static DataSet GetData(SqlCommand cmd)
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
        using (SqlDataAdapter sda = new SqlDataAdapter())
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
                return ds;

Above you can see I am executing a simple select query on the Customers table of the Northwind database using the GetData function DataSet object. This dataset object is later on converted to XML using the GetXml() method in the web method.
Client side functionality
<script type="text/javascript" src="">script>
<script type="text/javascript">
    $(function () {
            type: "POST",
            url: "Default.aspx/GetCustomers",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
            error: function (response) {
    function OnSuccess(response) {
        var xmlDoc = $.parseXML(response.d);
        var xml = $(xmlDoc);
        var customers = xml.find("Table");
        var row = $("[id*=gvCustomers] tr:last-child").clone(true);
        $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
        $.each(customers, function () {
            var customer = $(this);
            $("td", row).eq(0).html($(this).find("CustomerID").text());
            $("td", row).eq(1).html($(this).find("ContactName").text());
            $("td", row).eq(2).html($(this).find("City").text());
            row = $("[id*=gvCustomers] tr:last-child").clone(true);

In the above code snippet a jQuery AJAX call is made to the Web method GetCustomers in the document ready event of the page. The web method then returns an XML string which is parsed using the jQuery parseXML method. Once the XML document is created rows are created and appended to the ASP.Net GridView control.
The below screenshot displays the ASP.Net GridView control which has been populated using jQuery AJAX and ASP.Net Web methods.
Bind GridView using DataSet and jQuery in ASP.Net

View Demo