This project is read-only.

Help For you: Get Selected Feature FID and attribute

Sep 6, 2011 at 10:30 PM
Edited Sep 6, 2011 at 10:37 PM

hi all.

this code is for get selected Feature FID:



       private void map1_SelectionChanged(object sender, EventArgs e)
            //  DotSpatial.Data.IFeature feat = map1.GetFeatureLayers()[0].DataSet.GetFeature(0);
            //  this.Text = feat.DataRow.ItemArray.GetValue(0).ToString();
            List<IFeature> ls1 = new List<IFeature>();
            FeatureLayer fl1 = map1.Layers[0] as FeatureLayer;
            ISelection il1 = fl1.Selection;
                ls1 = il1.ToFeatureList();
                this.Text = "";
                for (int i = 0; i < il1.Count; i++)
                    this.Text += ls1[i].Fid.ToString()+" , "; //FID
                    this.Text += ls1[i].DataRow.ItemArray.GetValue(0).ToString() + " , "; //attribute 


Good luck.

Oct 13, 2011 at 12:53 AM
Edited Oct 13, 2011 at 1:10 AM

Thank you itmicro!

Send more like this!!!



Oct 13, 2011 at 6:41 AM

well done bro!!! Although I went backwards to using MapWinGIS 6 to implement this function after so many days of trying and looking through documentations, I will however try it to see how it works. Thanks buddy!!! Nice code too :)

Jul 3, 2012 at 4:42 AM

well guys, I meet problem with this code while I get the fid from the mappointlayer! I selected some point in the mappointlayer then use your code to get the fid,but there is one feature return the fid equals -1? how this happened? I will very appreciate with anyone's help!

Jul 3, 2012 at 11:39 AM

if you open the attribute table of this layer manually in the legend control before running your code, check whats the FID there. if its 0 then fine and if it is -1 then the shapefile is corrupt i guess.

Jul 4, 2012 at 9:33 AM

well, if the file is corrupt, then can the file be added to the map using the map.addlayer()? Thanks!

Oct 29, 2012 at 10:25 PM
Edited Oct 29, 2012 at 10:27 PM

The file has no problems, the problem is ISelection, I have tried to solve the problem and the only thing I've accomplished is through code load and unload shapefile and the problem is solved FID >=0 ...

Using the FID RecordNumber instead, there is no assurance that the run of each element indexshape and the file may be damaged...

I tried the following but I used to map the selection on this fine but the FID becomes -1 for all elements of the selection shape...


Dim fLayer As  FeatureLayer
.'Here my code


anybody solution the problem?

Oct 30, 2012 at 2:50 AM

you can go to look into the source code for the map.functionmode=functionmode.identify;

Oct 30, 2012 at 5:03 PM

ok I will review and comment how me was...

Thanks lzz0517......

Oct 31, 2012 at 5:30 AM
Edited Oct 31, 2012 at 5:32 AM

I look into the source code for the functionmode.identify in DotSpatial.Controls.FeatureIdentifier and that helped me a lot thanks lzz0517....  :D

I post the code for anyone who has had the same problem FID = -1

Dim fLayer As  FeatureLayer
            'Here code for load fLayer
If Not IsNothing(fLayer) Then
  Dim result As List(Of IFeature) = New List(Of IFeature)()
  For Each feature As IFeature In result
    If Not IsDBNull(feature.ShapeIndex) Then
    End If
End If