0

[![enter image description here][1]][1]I'm trying to write a C# .NET add-in for ArcMap and I need some help. I want to be able to select a set of features in an SDE and then click a button that would add the selected features' field to a data grid view. I have been able to have selected features be added to a list box with a button but I can't modify the code to be a data grid. Below is my code so far after I've set up the SDE workspace.

        IEnumFeature pEnumFeat = (IEnumFeature)Pmap.FeatureSelection;
        IFields fields;
        pEnumFeat.Reset();
        IEnumFeatureSetup enumFeatSetup = (IEnumFeatureSetup)pEnumFeat;
        enumFeatSetup.AllFields = true;
        try
        {
            IFeature pfeat = pEnumFeat.Next();


            while (pfeat != null)
            {
                fields = pfeat.Fields;
                int x = fields.FindField("NAME OF FIELD");
                list.Add(pfeat.get_Value(x).ToString());
                Debug.Print(pfeat.get_Value(x).ToString());


                if (pfeat.get_Value(x).ToString() != "")
                    LstBxWellList.Items.Add(pfeat.get_Value(x).ToString());

                pfeat = pEnumFeat.Next();
            }
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
            return;
        }
  • Are you able to add a single "hello world" string to your datagridview? – Kirk Kuykendall Jun 22 '16 at 20:42
  • If my answer doesn't work, or isn't detailed enough, maybe post some code you've tried for adding the data to the DataGridView? – karl Jun 22 '16 at 20:49
1

It think this is the simplest way to add each value in a loop, rather than binding the DataGridView to a list. Your screenshot looks like you already have a column defined, if that's the case, ignore the first line:

myDataGridView.Columns.Add("Wellname", "Wellname");
while (pfeat != null)
{
    fields = pfeat.Fields;
    int x = fields.FindField("NAME OF FIELD");
    if (pfeat.get_Value(x).ToString() != "")
    {
         MyDataGridView.Rows.Add(pfeat.get_Value(x).ToString());
    }
    pfeat = pEnumFeat.Next();
}    
  • Thank you Karl, the code allowed for a selected feature's field to be added to the data grid view. I didn't have to use myDataGridView.Columns.Add("Wellname", "Wellname"); becuse I had already added the column in the form design but everything else worked as intended. – GISense Jun 27 '16 at 15:41
  • I'm working more on this and I'm now trying to add multiple fields into a new already defined column. Would I just need to copy/paste this below and name a different field, different int value and some how tell it to add to the same row but column 1? As of now this only adds a new row for each field and not into the next column. – GISense Jul 1 '16 at 19:55
  • You can create an object array and pass that to the MyDataGridView.Rows.Add method. I think that this would work: MyDataGridView.Rows.Add(new object[] {pfeat.get_value(x).ToString(), secondColumnValue, thirdColumnValue}); Some other options to consider here: stackoverflow.com/questions/10063770/… – karl Jul 7 '16 at 15:46
0

You should be able to set the Datasource for your DataGridView to your list. I would refresh afterwards, though I don't know if that's necessary or not:

myDataGridView.DataSource = list;
myDataGridView.Refresh();
  • I want to go in the direction of not having a list at all and just having the datagridview populated with a field of a selected feature. I'm fairly new to ArcObjects so I havent been able to write anything beyond the code for adding the selected feature to a list. – GISense Jun 22 '16 at 21:25
  • I'm assuming that in the line where you have: list.Add(pfeat.get_Value(x).ToString()); that "list" refers to a List<string> data structure. You could use that and bind it to the DataGridView as in this answer, but still skip the ListBox. But I'll post an alternative solution as another answer. – karl Jun 23 '16 at 14:11

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.