Mona Coder
Mona Coder

Reputation: 6316

How to add field to a Shapefile using DotSpatial?

I tried searching the web to find a sample of showing how to add a Field to attribute table of an existing shapefile. For example I have a Shapefile at

C://data/Streets.shp

and need to add two field L_CITY and R_CITY both text and 50 characters limit. How can I do this in DotSpatial?

Upvotes: 1

Views: 547

Answers (1)

Ted
Ted

Reputation: 3260

The first thing you need to do is add a reference to System.Data. Otherwise, the type definition for the DataTable is not available and it may not be obvious what you can do to modify the schema.

Then you can use standard DataTable programming like the following code:

    public void AddFieldExample()
    {
        IFeatureSet fs = FeatureSet.OpenFile("C:\\YourShapefile.shp");
        DataTable table = fs.DataTable;
        DataColumn lCity = table.Columns.Add("L_CITY");
        lCity.MaxLength = 50;
        DataColumn rCity = table.Columns.Add("R_CITY");
        rCity.MaxLength = 50;

    }

Upvotes: 1

Related Questions