DataGrid for WPF and Silverlight Overview > [No Target Defined] > [No Target Defined] > Step 2 of 3: Adding a Web Service |
In this step you'll add a data source to your project, and begin the process of binding the grid.
To set up your project, complete the following steps:
1. Navigate to the Solution Explorer, right-click the StealthPaging.Web project, and select Add Reference from the context menu.
1. In the Add Reference dialog box locate the System.Runtime.Serialization assembly and click the OK button to add a reference to your project. The dialog box will close and the reference will be added.
2. In the Solution Explorer right-click the StealthPaging.Web project, and select Add | New Item.
3. In the left pane of the Add New Item dialog box, select the Web item.
4. In the templates list, select Web Service, name the Web Service "DataWebService.asmx", and click the Add button. Note that the Web Service file will be added to your project and automatically opened.
5. In the DataWebService.asmx file, add the following using statements at the top of the file:
· Visual Basic
Imports System.Runtime.Serialization
· C#
using System.Runtime.Serialization;
6. In the DataWebService.asmx file, replace the code in the StealthPaging.Web namespace with the following::
· Visual Basic
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class DataWebService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetData(startRow As Integer, endRow As Integer) As List(Of ServerPerson)
Dim personList As New List(Of ServerPerson)()
For i As Integer = startRow To endRow - 1
personList.Add(New ServerPerson() With { _
.FirstName = String.Format("First Name {0}", i), _
.LastName = String.Format("Last Name {0}", i), _
.Age = i, _
.City = String.Format("City {0}", i) _
})
Next
Return personList
End Function
End Class
<DataContract> _
Public Class ServerPerson
Private _firstName As String
<DataMember> _
Public Property FirstName() As String
Get
Return _firstName
End Get
Set
_firstName = value
End Set
End Property
Private _lastName As String
<DataMember> _
Public Property LastName() As String
Get
Return _lastName
End Get
Set
_lastName = value
End Set
End Property
Private _age As Integer
<DataMember> _
Public Property Age() As Integer
Get
Return _age
End Get
Set
_age = value
End Set
End Property
Private _city As String
<DataMember> _
Public Property City() As String
Get
Return _city
End Get
Set
_city = value
End Set
End Property
End Class
· C#
namespace StealthPaging.Web
{
/// <summary>
/// Summary description for DataWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class DataWebService : System.Web.Services.WebService
{
[WebMethod]
public List<ServerPerson> GetData(int startRow, int endRow)
{
List<ServerPerson> personList = new List<ServerPerson>();
for (int i = startRow; i < endRow; i++)
{
personList.Add(new ServerPerson()
{
FirstName = string.Format("First Name {0}", i),
LastName = string.Format("Last Name {0}", i),
Age = i,
City = string.Format("City {0}", i)
});
}
return personList;
}
}
[DataContract]
public class ServerPerson
{
private string _firstName;
[DataMember]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
[DataMember]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private int _age;
[DataMember]
public int Age
{
get { return _age; }
set { _age = value; }
}
private string _city;
[DataMember]
public string City
{
get { return _city; }
set { _city = value; }
}
}
}
This code will create a new list that will be used to populate the C1DataGrid control.
7. Save your application, right-click the StealthPaging.Web project, and select Build from the context menu. Note that you'll now be done with the StealthPaging.Web project and will return to working with the StealthPaging project.
What You've Accomplished
In this step you've added a data source to your project and created a Web Service. In the next step you'll finish connecting the Web Service to your project and you'll run your application.