WebDataGrid Manual CRUD

Purpose

WebDataGrid (WDG) has the capability to update the DataSource automatically if AutoGRUD property of EditingCore is set to true. What is “Manual CRUD”? This is a technique used by developers to persist all changes made in the grid UI to the DataSource by their own custom code. In this article I will go through the steps required to get WDG involved in Manual Crud scenario. I will demonstrate the setup of each behavior needed in order to make this happen. I will show how to use Editor Providers with AddNew Row feature also.

Preview

Image1

Required background

The article assumes you are familiar with WebDataGrid component. If you are not, consider the starting point at this link.

Prerequisites

If you intend to work with the article you will need the following:

  • Visual Studio 2010/2008
  • NetAdvanage for ASP.NET 11.2

Steps Overview

  1. Create new empty website in Visual Studio
  2. Setup your DataSource( in this step you can download the sample from the end of the article and copy App_Folder from the sample into your project)
  3. Create an Entity “Employee” and add the XMLParser class
  4. Drag WebDataGrid component form the toolbox onto the page
  5. Setup grid’s columns
  6. Setup RowAdding Behavior
  7. Setup RowDeleting Behavior
  8. Setup RowEditTemplate
  9. Handle CRUD operations on the server

Steps

  1. Open Visual Studio and create empty ASP.NET WebSite.
  2. Setup your DataSource. In this step I’ve created two classes, the first one is called “Employee” and the second one is XMLParser static class. The second is used as a bridge between underling DataSource, which is simply xml file,  and WDG.

The employee class definition:

  1. public class Employee
  2.     {
  3.         public string Name { get; set; }
  4.         public string Email { get; set; }
  5.         public string Team { get; set; }
  6.         public string Level { get; set; }
  7.         public string Office { get; set; }
  8.     }

The XMLParser class definition:

  1. /// <summary>
  2. /// Summary description for XmlParser
  3. /// </summary>
  4. public static class XmlParser
  5. {
  6.  
  7.     public static List<Employee> GetAllEmployees(string xmlFilePath)
  8.     {
  9.         try
  10.         {
  11.             List<Employee> employees = new List<Employee>();
  12.  
  13.             Object obj = new object();
  14.             XDocument document = null;
  15.  
  16.             lock (obj)
  17.             {
  18.                 document = XDocument.Load(xmlFilePath);
  19.             }
  20.             foreach (var element in document.Elements().Elements(“employees”).Elements(“employee”))
  21.             {
  22.                 employees.Add(new Employee()
  23.                 {
  24.                     Name = element.Element(“name”).Value,
  25.                     Email = element.Element(“email”).Value,
  26.                     Level = element.Element(“level”).Value,
  27.                     Team = element.Element(“team”).Value,
  28.                     Office = element.Element(“office”).Value
  29.                 });
  30.  
  31.             }
  32.  
  33.             return employees;
  34.         }
  35.         catch (Exception)
  36.         {
  37.             throw;
  38.         }
  39.  
  40.     }
  41.  
  42.     public static string GetFileOfAllEmployees(string path)
  43.     {
  44.         try
  45.         {
  46.             Object obj = new object();
  47.  
  48.             lock (obj)
  49.             {
  50.                 return XDocument.Load(path).ToString();
  51.             }
  52.         }
  53.         catch (Exception ex)
  54.         {
  55.             throw ex;
  56.         }
  57.     }
  58.  
  59.     public static XElement LoadFile(string path)
  60.     {
  61.         try
  62.         {
  63.             Object obj = new object();
  64.  
  65.             lock (obj)
  66.             {
  67.                 return XElement.Load(path);
  68.             }
  69.         }
  70.         catch (Exception ex)
  71.         {
  72.             throw ex;
  73.         }
  74.     }
  75.  
  76.     public static void SaveFile(XElement file, string path)
  77.     {
  78.         try
  79.         {
  80.             Object obj = new object();
  81.  
  82.             lock (obj)
  83.             {
  84.                 file.Save(path);
  85.             }
  86.         }
  87.         catch (Exception ex)
  88.         {
  89.             throw ex;
  90.         }
  91.     }
  92. }

Here is the definition of the XML file used as a persistent store:

  1. <?xml version=1.0 encoding=utf-8?>
  2. <team>
  3.   <configuration></configuration>
  4.   <employees>
  5.     <employee>
  6.       <office>US</office>
  7.       <name>Aaron</name>
  8.       <email>Aaron@company.com</email>
  9.       <level>0</level>
  10.       <team>XAML</team>
  11.     </employee>
  12.   </employees>
  13. </team>

      

        3. In this step drag the WDG from the Visual Studio toolbox on the page. If the controls are not in your toolbox even after you have installed them, here is a link which explains how to add the controls to the toolbox.

Image2

          5. In this step we will setup the WDG’s columns. Note that we set AutoGenerateColumns property to false, which means that over the data binding stage the grid will try to match the properties of the Entity from the DataSource that it is bound to, in our case “Employee” class. The key for each BoundDataField must match the property from the Employee class.

  1. <ig:WebDataGrid ID=”WebDataGrid1″ runat=”server” Width=”100%” AutoGenerateColumns=”False”
  2.       Height=”450px” StyleSetName=”IG”>
  3.       <Columns>
  4.           <ig:BoundDataField Key=”Name”>
  5.               <Header Text=”Name” />
  6.           </ig:BoundDataField>
  7.           <ig:BoundDataField Key=”Email”>
  8.               <Header Text=”Email” />
  9.           </ig:BoundDataField>
  10.           <ig:BoundDataField Key=”Level” DataType=”System.String”>
  11.               <Header Text=”Level” />
  12.           </ig:BoundDataField>
  13.           <ig:BoundDataField Key=”Team”>
  14.               <Header Text=”Team” />
  15.           </ig:BoundDataField>
  16.           <ig:BoundDataField Key=”Office”>
  17.               <Header Text=”Office” />
  18.           </ig:BoundDataField>
  19.       </Columns>

              6. In this step we will setup RowAdding Behavior (Feature) of the grid. This behavior allows the developer to add new rows to the WDG and persist them later when they are posted to the server either via full or ajax (partial) postback. The setup looks like this:

  1. <ig:RowAdding Alignment=”Top” EditModeActions-EnableOnActive=”true”>
  2.             <EditModeActions EnableOnActive=”True”/>         
  3.             <ColumnSettings>
  4.                 <ig:RowAddingColumnSetting ColumnKey=”Level”
  5.                     EditorID=”WebDataGrid1_DropDownProviderLevel”
  6.                     DefaultValueAsString=”Level” />
  7.                 <ig:RowAddingColumnSetting ColumnKey=”Team”
  8.                     EditorID=”WebDataGrid1_DropDownProviderTeam”
  9.                     DefaultValueAsString=”Team” />
  10.                 <ig:RowAddingColumnSetting ColumnKey=”Office”
  11.                     EditorID=”WebDataGrid1_DropDownProviderOffice”
  12.                     DefaultValueAsString=”Office” />
  13.             </ColumnSettings>
  14.         </ig:RowAdding>

As you might have already noticed in ColumnSettings tag I’ve specified Editor providers for few of the columns. The editor providers allow developers to edit a particular column through a given control. In our case the “Level” column is edited by DropDownEditor Provider. You easily can spot how the association is done through ColumnKey and EditorID. If you are wondering how the setup of the DropDownProvider is implemented, here is the answer:

  1. <ig:DropDownProvider ID=”WebDataGrid1_DropDownProviderLevel”>
  2.     <EditorControl
  3.     DropDownContainerWidth=”170%” DropDownContainerHeight=”100px”
  4.     DropDownContainerMaxHeight=”200px” EnableAnimations=”False” EnableDropDownAsChild=”False”>
  5.         <Items>
  6.             <ig:DropDownItem Selected=”false” Value=”0″ Text=”0″>
  7.             </ig:DropDownItem>
  8.             <ig:DropDownItem Selected=”False” Value=”1″ Text=”1″>
  9.             </ig:DropDownItem>
  10.             <ig:DropDownItem Selected=”False” Value=”2″ Text=”2″>
  11.             </ig:DropDownItem>
  12.             <ig:DropDownItem Selected=”False” Value=”3″ Text=”3″>
  13.             </ig:DropDownItem>
  14.         </Items>  
  15.     </EditorControl>
  16. </ig:DropDownProvider>

As a result of the completed setup above, the grid will allow adding new employees to our DataSource using EditorProviders. Here is the result:

Image3

     7.  In this step we will setup RowDeleting Behavior. Similar to the RowAdding Behavior, it allows adding new rows to the WDG, the RowDeleting feature gives us the possibility to delete rows from the grid..

  1. <ig:RowDeleting />

Is it so simple? Yes it is, in order to enable this feature we need to add the above line of code to the Behaviors tag. To see this feature in action you may select grid row and press the Delete button from the keyboard. The row will be deleted from the UI and post will be issued to the server. Alex Kartavov blogged about “Confirm WebDataGrid Row Deletion with Message Box” while ago. It is considered to be a good practice prior to deletion of any rows from the grid to prompt the user to confirm the action.

         8. RowEditTemplate is a great feature of the grid that allows developers to edit the grid easily. All you need to get it working is to go to the smart tag of the control and select Behaviors, then enable the RowEditingTemplate and click apply.

RET

Once you’ve enabled RET(RowEditTemplate) you can double click on the row selector for a particular row and experience it.

RET1

            9. Here I will show you how the server operations are handled and data are saved into the data store which is XML file in our case. I will go in details only for RowAdding handler. The code looks like this:

  1. void EditingCore_RowAdding(object sender, RowAddingEventArgs e)
  2. {
  3.     XElement emp = new XElement(“employee”,
  4.                     new XElement(“office”, 1),
  5.                     new XElement(“name”, 2),
  6.                     new XElement(“email”, 3),
  7.                     new XElement(“level”, 4),
  8.                     new XElement(“team”, 5)
  9.     );
  10.  
  11.     emp.Element(“name”).Value = e.Values[“Name”].ToString();
  12.     emp.Element(“office”).Value = e.Values[“Office”].ToString();
  13.     emp.Element(“email”).Value = e.Values[“Email”].ToString();
  14.     emp.Element(“level”).Value = e.Values[“Level”].ToString();
  15.     emp.Element(“team”).Value = e.Values[“Team”].ToString();
  16.  
  17.     XElement file = XmlParser.LoadFile(Server.MapPath(@”App_data\Employees.xml”));
  18.  
  19.     XElement element = new XElement(“employee”);
  20.     //element.
  21.     file.Element(“employees”).Add(emp);
  22.  
  23.     //file.Elements().Elements(“employees”).First()
  24.     try
  25.     {
  26.         file.Save(Server.MapPath(@”App_data\Employees.xml”));
  27.     }
  28.     catch (Exception)
  29.     {
  30.         throw;
  31.     }
  32.     finally
  33.     {
  34.         string path = Server.MapPath(@”App_data\Employees.xml”);
  35.         this.WebDataGrid1.DataSource = GetAllEmployees(path);
  36.         this.WebDataGrid1.DataBind();
  37.     }
  38. }

In this handler I update the xml file with the newly added record.

Hope you enjoy it !

Related Topics

http://help.infragistics.com/NetAdvantage/ASPNET/2011.2/CLR4.0/?page=Web_WebDataGrid_WebDataGrid.html

http://samples.infragistics.com/aspnet/ComponentOverview.aspx?cn=data-grid

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s