Accessing the dbf file

2 posts, 0 answers
  1. Thomas
    Thomas avatar
    2 posts
    Member since:
    Nov 2010

    Posted 08 Dec 2010 Link to this post

    Is it possible to access the dbf file during the binding (see below):




    .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?





  2. Andrey
    Andrey avatar
    1681 posts

    Posted 10 Dec 2010 Link to this post

    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) 
    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 

    Andrey Murzov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
Back to Top