How to add field to a Shapefile using DotSpatial?

520 Views Asked by At

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?

1

There are 1 best solutions below

0
Ted On

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;

    }