In this article I will explain how to populate GridView using WCF service or in other words bind data to GridView using WCF Service in ASP.Net using C# and VB.Net.
I have made use of the following table Customers with the schema as follows.
Bind GridView using data from WCF Service in ASP.Net
I have already inserted few records in the table.
Bind GridView using data from WCF Service in ASP.Net
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
Adding Service to Project
The very first thing you need to do is add a WCF service to your project by clicking on Add New Items as shown below.
Bind GridView using data from WCF Service in ASP.Net
Building the WCF Service
The next task is to add the OperationContract method to the WCF Service that will fetch the data records from the database and will act as a source of data for the GridView.
You will need to import the following namespaces.
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.ServiceModel;
using System.Runtime.Serialization;
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.ServiceModel
Imports System.Runtime.Serialization
IService Interface
The IService Interface class has a DataContract class named CustomerData which contains a DataTable Property CustomersTable which will be used to send the data from the WCF Service to the Web Application.
The IService Interface has a method GetCustomers which is decorated with OperationContract attribute.
public interface IService
    CustomerData GetCustomers();
public class CustomerData
    public CustomerData()
        this.CustomersTable = new DataTable("CustomersData");
    public DataTable CustomersTable { get; set; }
<ServiceContract()> _
Public Interface IService
    <OperationContract()> _
    Function GetCustomers() As CustomerData
End Interface
<DataContract()> _
Public Class CustomerData
    Public Sub New()
        Me.CustomersTable = New DataTable("CustomersData")
    End Sub
    <DataMember()> _
    Public Property CustomersTable() As DataTable
            Return m_CustomersTable
        End Get
        Set(value As DataTable)
            m_CustomersTable = value
        End Set
    End Property
    Private m_CustomersTable As DataTable
End Class
Service Class
The IService Interface has been implemented in a class named Service which contains the definition of the GetCustomers method.
This method gets the records from the Customers table and populates the DataTable of the CustomerData class object which finally is returned to the client accessing the WCF service.
public class Service : IService
    public CustomerData GetCustomers()
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers"))
                using (SqlDataAdapter sda = new SqlDataAdapter())
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                        CustomerData customers = new CustomerData();
                        return customers;
Public Class Service
    Implements IService
    Public Function GetCustomers() As CustomerData Implements IService.GetCustomers
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
               Using sda As New SqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Using dt As New DataTable()
                        Dim customers As New CustomerData()
                        Return customers
                    End Using
                End Using
            End Using
        End Using
    End Function
End Class
Adding Reference of WCF Service
The next task is to add the Service Reference of the WCF service to the project so that it can be used to populate the GridView.
1. Right Click the Project in the Solution Explorer and click Add Service Reference from the context menu.
Bind GridView using data from WCF Service in ASP.Net
2. Then you need to locate the WCF Service in the project using the Discover button and then press OK to add its Service Reference.
Bind GridView using data from WCF Service in ASP.Net
Bind GridView using data from WCF Service
HTML Markup
The HTML Markup consists of an ASP.Net GridView.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" ItemStyle-Width="150" />
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
Inside the Page Load event, the GridView is populated with data from the WCF Service by accessing the GetCustomers method using the ServiceClient object.
protected void Page_Load(object sender, EventArgs e)
    ServiceReference.ServiceClient client = new ServiceReference.ServiceClient();
    GridView1.DataSource = client.GetCustomers().CustomersTable;
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim client As New ServiceReference.ServiceClient()
    GridView1.DataSource = client.GetCustomers().CustomersTable
End Sub
Bind GridView using data from WCF Service in ASP.Net