b

blu

Saturday, 15 December 2012

DataGrid in SilverLight Example C#


   DataGrid in SilverLight Example C#

        .Create a Silverlight  Project(including web application)

                      Silverlight 4, 5.

             .Add Datagrid and TextBlock to Grid Layout   as shown below


<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
       
<sdk:DataGrid AutoGenerateColumns="True" Height="221" HorizontalAlignment="Left" Margin="0,67,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="388" />
        <TextBlock FontSize="20" Foreground="Green" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="Silverlight Data Grid Demo" VerticalAlignment="Top" Width="356" FontWeight="Bold" FontStretch="Condensed" TextWrapping="Wrap" />

    </Grid>
</UserControl>



  . Add a class called  Customers with the following properties,

    public class Customers
    {
        public int custID { get; set; }
        public String Name { get; set; }
        public DateTime DOB { get; set; }
        public DateTime MemberSince { get; set; }
        public String emailID{get;set;}
    }

  .Fill  Customers List   with samples

void FillData()
        {
            custList.Add(new Customers { custID=1, DOB=new DateTime(1973,12,12), emailID="p_pathi2020@yahoo.com", MemberSince=new DateTime(2000,12,12), Name="Peter hans" });
            custList.Add(new Customers { custID = 2, DOB = new DateTime(1970, 12, 12), emailID = "sam@yahoo.com", MemberSince = new DateTime(2000, 12, 12), Name = "sam benegal" });
            custList.Add(new Customers { custID = 3, DOB = new DateTime(1983, 12, 12), emailID = "paul@yahoo.com", MemberSince = new DateTime(2000, 12, 12), Name = "Paul hons" });
            custList.Add(new Customers { custID = 4, DOB = new DateTime(1993, 12, 12), emailID = "pari@yahoo.com", MemberSince = new DateTime(2000, 12, 12), Name = "whiteman pari" });
            custList.Add(new Customers { custID = 5, DOB = new DateTime(1988, 12, 12), emailID = "pron@yahoo.com", MemberSince = new DateTime(2000, 12, 12), Name = "Porn star" });
        }

.Bind DataGird to Customer List

        List<Customers> custList = new List<Customers>();
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            FillData();

            dataGrid1.ItemsSource = custList;
        }


.Here is the Output


No comments:

Post a Comment