This walkthrough shows you how to use WebValueList in WebGrid.
During this walkthrough, you will learn how to do the following:
- Connect to a Microsoft Access database using Microsoft Jet 4.0 OLE DB Provider.
- Use OleDbDataAdapter to configure SQL Query and generate DataSet.
- Set Web Application's environment and the DataSource.
- Use WebGrid's InitializeDataSource event to bind the DataSet.
- Specify a WebValueList feature in a grid to replace the id with value.
Prerequisites
In order to complete this walkthrough, you will need the following:
- Access to the Microsoft Access NorthWind database.
- Visual Studio 2003 Application.
Step-By-Step Instructions
To create new web application and specify WebValueList in WebGrid.
- Binding Hierarchical DataSource to WebGrid.
- Drag OleDbDataAdapter from Data tab to the WebForm.
- Click Next until Generate SQL statements page, type in SELECT * FROM PRODUCTS in the textbox and click Next then Finish. Right click the newly created oleDbDataAdapter and select Generate DataSet, click OK.
- Rename the oleDbDataAdapter object to daProducts.
- Switch to HTML View and add the event OnInitializeLayout="WebGrid1_InitLayout" inside the <ISWebGrid:WebGrid id="WebGrid1" ... tag.
- Now open the code-behind editor by right clicking on the page and click View Code.
- Add using ISNet.WebUI.WebGrid; at the top of the code-behind page.
- Add following C# code:
C# Copy Code protected void WebGrid1_InitLayout(object sender, ISNet.WebUI.WebGrid.LayoutEventArgs e) { // set valuelist for ShipVia WebValueList vlShipVia = WebGrid1.GetTableByName("Orders").Columns.GetNamedItem("ShipVia").ValueList; Hashtable htShipVia = new Hashtable(); htShipVia.Add("1", "Speedy Express"); htShipVia.Add("2", "United Package"); htShipVia.Add("3", "Federal Shipping"); vlShipVia.DataSource = htShipVia; // set valuelist for Products WebValueList vlProd = WebGrid1.GetTableByName("Order Details").Columns.GetNamedItem("ProductID").ValueList; if (!vlProd.IsDataCached()) { daProducts.Fill(dsNorthWind1.Products); vlProd.DataSource = dsNorthWind1; }
vlProd.SetDataSource("Products", "ProductName","ProductID");}
- Set the WebGrid's Width to 600px, Height to 300px for larger display.
- Open WebGrid.NET Designer, expand RootTable, expand ChildTables, expand Orders table and select its Columns.
- Move the ShipVia column to the third column from the top.
- Click OK to apply all settings.
- Compile and run the WebForm.
References
WebValueList Class