This is a migrated thread and some comments may be shown as answers.

Accessing the dbf file

1 Answer 114 Views
Map
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 08 Dec 2010, 08:45 PM
Is it possible to access the dbf file during the binding (see below):

this

 

 

.StateLayer.Reader.DataSource = new Uri(string.Format(ShapeRelativeUriFormat, "usa_states.dbf"), UriKind.Relative);

I would like to append some text to each of the state names, if I could do some sort of loop / foreach that would be great. Can you provide sample code?

Regards
Thomas

 

 

 

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 10 Dec 2010, 06:30 PM
Hi Thomas,

When the shape reader reads the shape file (shp and dbf pair) then it creates the ExtendedData instance for each shape with a data from dbf file.
You can change values of properties the ExtendedData provides. Also you can add new property and then assign the value from any other source.
You can make it from a code for each read shape using the PreviewReadCompleted event. It occurs before the shapes are added to the information layer.
The sample code is below.
private const string NonDbfDataField = "SampleDataField"
  
void RadMap1_InitializeCompleted(object sender, EventArgs e) 
    if (!this.initialized) 
    
        this.initialized = true
  
        this.StateLayer.Reader = new MapShapeReader(); 
        this.StateLayer.Reader.PreviewReadCompleted += new PreviewReadShapesCompletedEventHandler(Reader_PreviewReadCompleted); 
  
        this.StateLayer.Reader.Source = new Uri(string.Format(ShapeRelativeUriFormat, "usa_states.shp"), UriKind.Relative); 
        this.StateLayer.Reader.DataSource = new Uri(string.Format(ShapeRelativeUriFormat, "usa_states.dbf"), UriKind.Relative); 
    
  
private void Reader_PreviewReadCompleted(object sender, PreviewReadShapesCompletedEventArgs eventArgs) 
    if (eventArgs.Error == null
    
        foreach (MapShape shape in eventArgs.Items) 
        
            this.SetAdditionalData(shape); 
        
    
  
private void SetAdditionalData(MapShape shape) 
    ExtendedData extendedData = shape.ExtendedData; 
    if (extendedData != null
    
        // add new property to ExtendedData 
        if (!extendedData.PropertySet.ContainsKey(NonDbfDataField)) 
        
            extendedData.PropertySet.RegisterProperty(NonDbfDataField, "", typeof(string), ""); 
        
  
        string stateName = (string)shape.ExtendedData.GetValue("STATE_NAME"); 
        string additionalFieldValue = this.GetDataByStateName(stateName); 
  
        // assign value to new property 
        shape.ExtendedData.SetValue(NonDbfDataField, additionalFieldValue); 
  
        // change existing property 
        shape.ExtendedData.SetValue("STATE_NAME", "State name: " + (string)shape.ExtendedData.GetValue("STATE_NAME")); 
    
  
private string GetDataByStateName(string stateName) 
    // returns additional field value 

Greetings,
Andrey Murzov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
Map
Asked by
Thomas
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or