Help For you: Get Selected Feature FID and attribute

Sep 6, 2011 at 9:30 PM
Edited Sep 6, 2011 at 9: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;
            try
            {
                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 
                }
          
          
             
            }
            catch
            {
            }
        }

 

Good luck.

Oct 12, 2011 at 11:53 PM
Edited Oct 13, 2011 at 12:10 AM

Thank you itmicro!

Send more like this!!!

Regards,

Evemar

Oct 13, 2011 at 5: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 3: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 10: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 8: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 9:25 PM
Edited Oct 29, 2012 at 9: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
.
.
					
fLayer.DataSet.InitializeVertices()
fLayer.AssignFastDrawnStates()
fLayer.DataSet.UpdateExtent()
fLayer.DataSet.Save()
fLayer.DataSet.InvalidateVertices()

 

anybody solution the problem?

Oct 30, 2012 at 1:50 AM

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

Oct 30, 2012 at 4:03 PM

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

Thanks lzz0517......

Oct 31, 2012 at 4:30 AM
Edited Oct 31, 2012 at 4: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)()
  result=fLayer.Selection.ToFeatureList()
  For Each feature As IFeature In result
    If Not IsDBNull(feature.ShapeIndex) Then
      MsgBox(fLayer.DataSet.ShapeIndices.IndexOf(feature.ShapeIndex))
    Else
      MsgBox(feature.Fid)
    End If
  Next
End If