Exploring Cool Features of Devexpress' ASPxGridView
Adding a Global Application Class
You need very little code to get data from your database into an ASPXGridView with XPO. Add a Global Application Class—global.asax—file to your web site. Select Website|Add New Item. From the Templates dialog, select the Global Application Class template. Click OK. This will add a global.asax file to your web site project (see Figure 3). After you add the needed code to the global.asax file, it will look like the code in Listing 2.
Click here for a larger image.
Figure 3: Add a global.asax file to your web site; this will let you add some code for application level events.
Listing 2: Startup code that establishes plumbing for using the XPO Data Source and generated code.
Imports System.Web.SessionState PublicClass Global_asax Inherits System.Web.HttpApplication Sub Application_Start(ByVal sender As Object, _ ByVal e As EventArgs) Dim connectionString As String = _ DevExpress.Xpo.DB.MSSqlConnectionProvider. _ GetConnectionString( _ "caspar\SQLExpress", "northwind") Dim dictionary As DevExpress.Xpo.Metadata.XPDictionary = _ New DevExpress.Xpo.Metadata.ReflectionDictionary() Dim store As DevExpress.Xpo.DB.IDataStore = _ DevExpress.Xpo.XpoDefault. _ GetConnectionProvider(connectionString, _ DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists) dictionary.GetDataStoreSchema(GetType _ (northwind.Products).Assembly) DevExpress.Xpo.XpoDefault.DataLayer = _ New DevExpress.Xpo. _ ThreadSafeDataLayer(dictionary, store) DevExpress.Xpo.XpoDefault.Session = Nothing End Sub Sub Session_Start(ByVal sender As Object, _ ByVal e As EventArgs) ' Fires when the session is started End Sub Sub Application_BeginRequest(ByVal sender As Object, _ ByVal e As EventArgs) ' Fires at the beginning of each request End Sub Sub Application_AuthenticateRequest(ByVal sender As Object, _ ByVal e As EventArgs) ' Fires upon attempting to authenticate the use End Sub Sub Application_Error(ByVal sender As Object, _ ByVal e As EventArgs) ' Fires when an error occurs End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the session ends End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the application ends End Sub End Class
The code in Listing 2 is boilerplate code that I got from tv.devexpress.com. The code contains helper methods for obtaining connection strings dynamically and creating a dictionary and datastore. You can copy the code in the Application_Start event and substitute the arguments to GetConnectionString for your database server and your database's name. You also need to provide the namespace and class name for the GetType argument of GetDataStoreSchema. This is the namespace and name of the generated XPO persistent classes. In the example, this is northwind.Products.
Using the ASPxGridView
You have written all the code you need for the demonstration—except one line of code. (After you get the grid view and XpoDataSource on the default.aspax page, you'll add the line of code.)
Next, open the design mode of the Default.aspx page. Drop an ASPxGridView control from the Toolbox window's DX.8.3:Data tab. Follow these steps to configure the ASPxGridView:
- After dropping the ASPxGridView control on the default.aspx page, use the same Toolbox tab (DX.8.3: Data) and drop an XpoDataSource on the default.aspx page.
- Using Figure 4 as a visual guide, configure the XpoDataSource by setting the ServerMode property to True and the TypeName to the name of the generated persistent class (which is northwind.Products on my PC and may include the project's namespace first).
- Next, select the ASPxGridView and set the DataSourceID property to be the XpoDataSource (in this example, the DataSourceID will be XpoDataSource1; see Figure 5).
- The final step is to add a Page_Init method that establishes session information for the XpoDataSource (see Listing 3.)
Page 3 of 6
This article was originally published on January 6, 2009