Change marker based on index

Topics: Help, Windows Forms
Sep 27, 2013 at 2:50 PM
I've got a c# program where I am adding markers to a map, and putting their location on a datagridview table. I've got it set up so that if you mouse over the marker, the marker changes color and the datagridview row is highlighted. This works perfectly. What I want is to go the other way - mouse over the datagridview row and highlight the marker.

The way I am picking the marker is I am going through a foreach loop for all of the markers and looking for where the coordinates match, then selecting that marker. It appears to work, in that I am selecting the correct marker, but it is not changing to color of the marker. I am using a custom marker called a MyMarker. Am I not passing my Highlight method the right instance of the marker? I'm lost!

Below is some of the code.
        public class MarkerSet_  //Develop the marker system class
            public int MarkerIndex = -1;  //The row number of the datagridview table
            public MyMarker marker;
        public MarkerSet_  MarkerSet = new MarkerSet_ ();

        void MainMap_OnMarkerEnter(GMapMarker item)
        {   ///This works great, and highlights both the marker and the datagridview row
                MarkerSet.MarkerIndex = MarkerIndex(item); 
//MarkerIndex(index) finds the row number in the datagridview table - works fine

                MarkerSet.marker = item as MyMarker;  //make this the current marker
                HighlightMarker(MarkerSet.marker);  //Highlight the current marker

        private void DataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            int index = e.RowIndex;
            MarkerSet.MarkerIndex = index;
            MarkerSet.marker = FindMarker(index);
//FindMarker is a method that identifies the marker to corresponds to the index, shown below

        public void HighlightMarker(GMapMarker item)
        {  //This works if called from OnMarkerEnter
            MyMarker rc = item as MyMarker;
            Pen tmpPen = new Pen(Brushes.Red, 5);
            rc.Pen = tmpPen;
            item = rc;
            DataGridView1.Rows[MarkerSet.MarkerIndex].DefaultCellStyle.BackColor = Color.White;

        MyMarker FindMarker(int index)
//round to 6 digits
            double Lat = Convert.ToDouble(dt.Rows[index][0]);  //items from the datagridview
            double Lng = Convert.ToDouble(dt.Rows[index][1]);
            PointLatLng p = new PointLatLng(Lat, Lng);

            foreach (GMapMarker mark in BaseMap.Overlays[0].Markers)
                double markLat = Math.Round(mark.Position.Lat, 6);
                double markLng = Math.Round(mark.Position.Lng, 6);
                if (Math.Round(p.Lat, 6) == markLat && Math.Round(p.Lng, 6) == markLng)
                    return mark as MyMarker; //This does return the correct marker

Sep 27, 2013 at 3:58 PM
search by coordinates is a bit strange... anyway try call map.Invalidate(); after you are done
Sep 27, 2013 at 4:02 PM
Thanks for the quick response! That solved it. What is a better way to find the marker index? I tried item.tag, but it came back null? I'm really just starting out with this, but it is super cool!