Tuesday, September 07, 2010

VE – using List of shapelayers to populate the gridview

Posted by david on 16. December 2008 23:07

I was googling some map code ( importshapelayer ) a couple days ago & was surprised ( shocked horrified ) to find one of these blog entries as the second or third result.  Damn – these are just disorganized notes as I muddle my way through this map stuff.  I really need to write more clearly … &, perhaps, turn on comments … maybe someone out there ( if anybody had the patience to plow through this madness ) maybe someone out there would have some answers …

Moving on - - -

these notes concern using a List<ShapeLayer> collection as a datasource for a gridview.  Also looking at LiNQ & List collections.  

Where I’m at:

I was able to use a datatable object as a datasource for my gridview in the 12/11 version.  That strategy has been bugging me, though: it creates a separate set, a duplicate set, a redundant set of data.  The datatable is just mirroring what’s on the map. 

  • page load
  • load map
  • load datatable ( at first only a row representing the baselayer )
  • update gridview with the datatable
  • add another layer ( the hurricane, or a kml file )
  • reload datatable with new data
  • update gridview with the datatable

 

what’s been buggin me is that the map can/should provide the same info. I am able to get a list of layers and shapes because that’s how I loaded the datatable.  Why should I create a separate dataset that only mirrors what I have.

So i went back to collections, the List<T> … which works, but I can’t yet figure out how to use the list as a datasource.  It’s probably right under my freakin nose. 

I am able to write:

List<ShapeLayer> LayerList = new List<ShapeLayer>(); 
List<Shape> ShapeList = new List<Shape>(); 
 
protected void Page_Load(object sender, EventArgs e) 
{ 
    Grid1.DataSource = LayerList; 
    Grid1.DataBind(); 
} 

Ok, The above is working.  I thought it wasn’t for a while, but turns out it was just taking a bit for the update to kick in.

The setup is this:

layerList, a List<ShapeLayer>, is defined ( initialized? set? ) for the page class:

   1: public partial class Storage_2008_12_14_2008_12_14 : System.Web.UI.Page
   2: {  
   3:     List<ShapeLayer> LayerList = new List<ShapeLayer>();

on PageLoad, Gridview datasource is set to LayerList, & a databind command is called to seal the deal. ( necessary yet? ) then updateLayerList method is called.

updateLayerList – clears the list, iterates through all of the shapelayers on the map, adding each shapelayer to layerList, then another databind is run.

code for pageload & for the updateLayerList method:

   1: public partial class Storage_2008_12_14_2008_12_14 : System.Web.UI.Page
   2: {  
   3:     List<ShapeLayer> LayerList = new List<ShapeLayer>(); 
   4:  
   5:     protected void Page_Load(object sender, EventArgs e)
   6:     {
   7:         // default title of baselayer is empty - correct that here for presentation
   8:         ScubaMap.GetShapeLayerByIndex(0).Title = "Base Layer";        
   9:         Grid1.DataSource = LayerList;
  10:         Grid1.DataBind();        
  11:         updateLayerList();              
  12:     }
  13: protected void updateLayerList()
  14: {
  15:     LayerList.Clear();
  16:     //// get the count of layers on the map       
  17:     int layercount = ScubaMap.GetShapeLayerCount();
  18:     //// iterate through the map layers, adding the layers to layerList
  19:     for (int i = 0; i < layercount; i++)
  20:     {            
  21:         LayerList.Add(ScubaMap.GetShapeLayerByIndex(i));
  22:         //if (!LayerList.Contains(ScubaMap.GetShapeLayerByIndex(i)))
  23:     }        
  24:     Grid1.DataBind();              
  25: }

This is functional ( 12/14 ) but I am getting unexpected behavior:

page loads, grid view loads, giving one record … the base layer. I know that the map data is not fully resolved because the list view does not show the layerID until the next refresh … which is every 10 sec or on the next button event.    When the refresh occurs, the gridview correctly shows the ID of the baselayer but the title reverts to “unsaved collection.” 

the updateLayerList method:

1: clears out the list

2: iterates through the map getting layer info

3: rebinds

the clear is happening at the List, I thought … & a specific layer Title should not be affected by the LayerList.Clear.  It is, though. 

next time -

You can just set the DataSource to be a List<MyObject> and DataBind. You
won't get any help with setting up the columns at design time if you do it
that way - you'll have type in all the details. If you want design time
help consider using an ObjectDataSource that points to a method that returns
the list.” (
source)

The ASP Column DataList vs. DataGrid in ASP.NET

Sort GridView with custom collection as DataSource - ASP.NET Forums

.NET ASP.NET Use gridview with generic collection

Northwind Meets Virtual Earth - Generate VE Maps with LINQ VB, using LINQ to generate the datasource for the map: “

“The key takeaway here is that in one LINQ statement we queried over multiple data sources, the Northwind Database and the geocoder.us service, to create a single XML document that conformed to the GeoRSS standard and passed that to the Virtual Earth service to generate our map. As you can see, it's pretty easy to create XML, in this case RSS, from multiple sources with LINQ and Visual Basic 9.”

Comments

Add comment


 

biuquote
Loading