SIGN UP MEMBER LOGIN:    
ARTICLE

Silverlight Datagrid - Retrieving details on Editing

Posted by Mahadesh Mahalingappa Articles | Silverlight with C# August 19, 2011
In this article we would be seeing how we are going to retrieve the details on Editing the DataGrid or Listbox.
Reader Level:
Download Files:
 

Created a new Project SLDatagridClick and

Add a DataModel as shown below to the Web Project.

Add DataModel in silverlight

DataModel Wizard in silverlight

Data Model is created as shown below :

DataModel  in silverlight
WCFService  in silverlight

Add a method to the DataService as shown below :

[OperationContract]
public IEnumerable<Persons> GetPersons()
    {
        DataEntities context = new DataEntities();
 
        var persons = from o in context.People
        select new
Persons
        {
            id = o.ID,
            firstName = o.FirstName,
            lastName = o.LastName
        };           
        return persons;
    }
 
public class Persons
    {
        public int id { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
    }


Add the Service Reference to the Silverlight Project.

Add Service Reference  in silverlight

Now lets get into the Silverlight project.

Modify the MainPage.xaml.cs as shown below :

using SLDatagridClick.DataServiceReference;
 
namespace SLDatagridClick
{
    public partial class MainPage :
UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DataServiceClient client = new DataServiceClient();
            client.GetPersonsCompleted += new EventHandler<GetPersonsCompletedEventArgs>(client_GetPersonsCompleted);
            client.GetPersonsAsync();
        }
 
        void client_GetPersonsCompleted(object sender, GetPersonsCompletedEventArgs e)
        {
            dg.ItemsSource = e.Result;
        }
    }
}


Now lets give it a run and see the output :

DataGrid Display  in silverlight

Now lets get to work. When I Edit the cell , I should be able to retrieve the modified cell contents. I have just tried to bring together some of the scenarios I came across while using Datagrid. In this article I would demonstrate one such scenario :

Lets add the code for that scenario :

Add the event handler :

dg.CellEditEnded += new EventHandler<DataGridCellEditEndedEventArgs>(dg_CellEditEnded);

Make use of the CellEditEnded.

Login to add your contents and source code to this article
share this article :
post comment
 

Hello Mahadesh! I'd like to ask you a question. Can I overwrite the datagrid's default behaviour about the Enter key? Now if i hit the enter key in the data grid the seleciton jumps down a row. But i want to perform some action in the selected item in the datagrid when i hit the enter. Can I do this or i have to make a new class based on the datagrid? Thanks for your help, Best regards, Dubi

Posted by Dubovszki Martin Oct 10, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor