Dynamic Datagrid creation with wcf service which help to take data from Database


This article will help to develop the DataGrid and display data from database using WCF service. This article will also explain the WCF and SliverLight relation or can say binding. It is a sample example.

Creating xmal(silverlight) page



Create new sliverlight application named as myproject. add xmal page named as MyTimeSheet.xmal.Design that page as follows.


& lt;data:DataGrid GridLinesVisibility="Horizontal" Height="Auto" Grid.Row="1" RowHeight="25" ScrollViewer.VerticalScrollBarVisibility="Auto" x:Name="dtgTimeSheet" Margin="0,0,3,20" IsReadOnly="True" RowBackground="#FFFFFFFF" AlternatingRowBackground="#FFf3f3f4" AutoGenerateColumns="False" HorizontalScrollBarVisibility="Auto" FontFamily="{StaticResource AIPFont}" FontSize="14" Grid.ColumnSpan="3" VerticalAlignment="Stretch" CanUserResizeColumns="False" HeadersVisibility="None" LoadingRow="dtgTimeSheet_LoadingRow" SelectedIndex="1" & gt;

& lt;data:DataGridTemplateColumn & gt;

& lt;data:DataGridTemplateColumn.CellTemplate & gt;
& lt;DataTemplate& gt;
& lt;HyperlinkButton Width="250" Click="Add_Click" Content="{Binding Topic}" ToolTipService.ToolTip="{Binding Topic}" /& gt;
& lt;/DataTemplate& gt;
& lt;/data:DataGridTemplateColumn.CellTemplate& gt;
& lt;/data:DataGridTemplateColumn& gt;

& lt;data:DataGridTemplateColumn & gt;
& lt;data:DataGridTemplateColumn.CellTemplate & gt;
& lt;DataTemplate& gt;
& lt;HyperlinkButton Width="50" Click="Add_Click" Content="{Binding Edit}" /& gt;
& lt;/DataTemplate& gt;
& lt;/data:DataGridTemplateColumn.CellTemplate>
& lt;/data:DataGridTemplateColumn& gt;


& lt;data:DataGridTextColumn Binding="{Binding TimeSheetID}" Width="80" Visibility="Collapsed" & gt;

& lt;/data:DataGrid.Columns& gt;
& lt;/data:DataGrid& gt;





Now we will develop the MyTimeSheet.xmal.cs


public static EndpointAddress endPoint = null;
TimesheetManagerService.TimeSheetManagerServiceClient objTimeSheetManger = new TimeSheetManagerServiceClient();


public myTimesheet()
{
objTimeSheetManger = null;
endPoint = new EndpointAddress(App.TimeSheetManagerServiceURI);
objTimeSheetManger = new TimeSheetManagerServiceClient(App.bind, endPoint);

objTimeSheetManger.GetReservationsCompleted += new EventHandler(objTimesheetManger_GetReservationsCompleted);
objTimesheetManger.GetReservationsAsync(EmployeeID);

}


void objTimeSheetManger_GetReservationsCompleted(object sender, GetReservationsCompletedEventArgs e)
{
try
{
dtgTimeSheet.ItemsSource = null;
dtgTimeSheet.ItemsSource = e.Result;

//dtgUsers.Focus();
}
catch (Exception ex)
{
}
}


now open the App.xmal.cs and modify the code

public static EndpointAddress endPoint = null;
public static BasicHttpBinding bind = new BasicHttpBinding();
public static string TimeSheetManagerServiceURI = string.Empty;
string url = HtmlPage.Document.DocumentUri.ToString();
string ServiceAddress = string.Empty;

string[] URLSplit = url.Split("/".ToCharArray());
ServiceAddress = URLSplit[2];
TimeSheetManagerServiceURI = "http://" + ServiceAddress + "/Service/TimeSheetManagerService.svc";







Now we will create the WCF service code.
goto project and say add new wcf service named as TimeSheetManagerService.svc

it will create unterface also.
so first write the code in ITimeSheetManagerService


[ServiceContract]
public interface ITimeSheetManagerService
[ {
[OperationContract]
List GetReservations();
}


[DataContract]
public class ReservationBinding
{
[DataMember]
public string Time { get; set; }

[DataMember]
public string DisplayTime { get; set; }

[DataMember]
public string Topic { get; set; }
[DataMember]
public Int64 TimeSheetID { get; set; }
}




NOW will developed code for service TimeSheetManagerService.svc

///create the object of clas which does the data manipulation

public List GetReservations()
{

List lstReservations = new List();
DataSet ds = [name of class object].GetSchedule();
if (ds.Tables[0].Rows.Count > 0)
{
reservationBinding.Time=ds.Tables[0].Rows[0]["Time"].ToString();
reservationBinding.Topic=ds.Tables[0].Rows[0]["Topic"].ToString();
reservationBinding.timesheetid=ds.Tables[0].Rows[0]["timesheetid"].ToString();
lstReservation.Add(reservationBinding);

}
return lstReservationBinding;
}


Comments

Guest Author: Bobcat17 Sep 2013

There's a terrific amount of knowledge in this article!



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: