3

I tried a lots on Google 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. Can you please let me know how to do this in DotSpatial?

2

You would need to access the attribute table of the Shapefile, then get the column collection and add a new one:

static void AddColumn()
{
    string sf_name = @"C:\temp\a.shp";

    //Open
    Shapefile sf = Shapefile.OpenFile(sf_name);

    //Create the new column (name + type)
    DataColumn newCol = new DataColumn("colName", typeof(string));
    //Add the length restriction
    newCol.MaxLength = 50;

    //Add the new column to the attribute table.
    sf.Attributes.Table.Columns.Add(newCol);

    // save in the current shapefile
    sf.Save();

    //Or save in a new one
    sf.SaveAs(@"C:\temp\a_copy.shp", true);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.