Select to view content in your preferred language

How to select datagridview rows based on selected features?

978
2
06-13-2014 08:08 AM
BaghdadAlAlavi
Deactivated User
Hi all
I've used sample "Bind a geodatabase table to .net control" to show attribute table of feature layers in a datagridview.
Now, i wanna update that datagridview when user selects some features in map and select corresponding rows.
Is there any code or sample for that? I've searched all internet but no help.

Regards
0 Kudos
2 Replies
BrentHoskisson
Frequent Contributor
Unfortunately there is no "automatic" code.  ArcObjects is COM based and created before .NET came into being.  You will have to create your own binding code.  There is hope as Arc GIS Professional is supposed to be .NET based, but whether we will be able to load feature classes as data sets is still a big question mark.

Good Luck
0 Kudos
AhmedEl-Sisi
Deactivated User
Hi all
I've used sample "Bind a geodatabase table to .net control" to show attribute table of feature layers in a datagridview.
Now, i wanna update that datagridview when user selects some features in map and select corresponding rows.
Is there any code or sample for that? I've searched all internet but no help.

Regards


I have implemented a function based on ""Bind a geodatabase table to .net control"" sample, use this function when selection changed (may be in ActiveView Selection Changed).
You should pass your featurelayer and ObjectID field as parameters.
you should modify the code/parameters according to your business.
private void UpdateGridSelection(IFeatureLayer featureLayer,string oIDField)
{
    try
    {
        //Clear Selection
        dataGridView1.ClearSelection();
        //Get Selection set and populate Object IDs in List
        IFeatureSelection pFeatureSelection = featureLayer as IFeatureSelection;
        ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;
        IEnumIDs IDsEnum = pSelectionSet.IDs;
        int Id = -1;
        Id = IDsEnum.Next();
        List<int> IdList = new List<int>();
        while (Id != -1)
        {
            IdList.Add(Id);
            Id = IDsEnum.Next();
        }
        //Select Rows Matches Selected Features By Its Object ID
        if (IdList.Count > 0)
        {
            var rows = from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                       where IdList.Contains(Convert.ToInt32(row.Cells[oIDField].Value))
                       select row;
            foreach (var item in rows)
            {
                item.Selected = true;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}


Regards,
Ahmed
0 Kudos