Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to know what is the easiest way to set the starting location of a data dialog to a IWorkspace?

My problem is that basically I do not want a connection (.sde file) for the users. I want to set the starting location to a workspace connected via IPropertySet. Is it possible?

This is related: Force a new AddDataDialog to open at a defined directory path but does not help.

EDIT:

following Kirk's suggestion, here's a code snippet. This does not work.

    public static IDataset OpenDatasetDialog(IWorkspace workspace,int handle)
    {
        var workspaceName = (IWorkspaceName)((IDataset)workspace).FullName);
        var gxDatabase = new GxdatabaseClass();
        gxDatabase.WorkspaceName = workspaceName;

        // we need to set a different variable as .NET runtime does not allow ref parameters with casts
        object gxObj = (object)gxDatabase;

        IGxDialog openDialog = new GxDialogClass();
        openDialog.RememberLocation = false;
        openDialog.set_StartingLocation(ref gxObj);
        var filterColletion = (IGxObjectFilterCollection)openDialog;
        filterColletion.AddFilter(new GxFilterTablesAndFeatureClassesClass(), true);
        openDialog.AllowMultiSelect = false;
        IEnumGxObject datasets = null;
        IDataset dataset = null;

        if (openDialog.DoModalOpen(0, out datasets) && datasets != null)
        {
            IGxObject gxObject = datasets.Next();
            if (gxObject is IGxDataset)
            {
                IGxDataset gxDataset = (IGxDataset)gxObject;
                dataset = gxDataset.Dataset;
            }
            else
                throw new ArgumentException();
        }
        openDialog.InternalCatalog.Close();
        return dataset;
    }
share|improve this question

2 Answers 2

up vote 4 down vote accepted

Kirk's answer is on the right track. The only thing missing is properly adding the GxDatabase into the dialog's internal catalog tree.

See the code below. The OpenGxDialogForRemoteWorkspace returns an enumerable of selected objects (if multiselect is allowed) or null in case the dialog is cancelled. It's very basic since I omitted the object filters you used, but I think you get the idea.

    private static IEnumerable<IGxObject> OpenGxDialogForRemoteWorkspace(IWorkspaceName workspaceName, int handle)
    {
        if (workspaceName == null) throw new ArgumentNullException("workspaceName");
        if (workspaceName.Type != esriWorkspaceType.esriRemoteDatabaseWorkspace)
        {
            throw new ArgumentException("Remote worskpace is expected.", "workspaceName");
        }

        var gxDatabase = new GxDatabaseClass();
        gxDatabase.WorkspaceName = workspaceName;

        var gxDialog = new GxDialogClass();
        var catalog = gxDialog.InternalCatalog;

        var topLevelCatalogItems = AsEnumerable(((IGxObjectContainer)catalog).Children);
        var remoteDatabaseFolder = topLevelCatalogItems.OfType<IGxRemoteContainer>().First();

        ((IGxObjectContainer)remoteDatabaseFolder).AddChild(gxDatabase);

        gxDialog.RememberLocation = false;

        object gxDatabaseObject = gxDatabase;
        gxDialog.set_StartingLocation(ref gxDatabaseObject);

        IEnumGxObject selection;
        if (gxDialog.DoModalOpen(handle, out selection))
        {
            return selection != null ? AsEnumerable(selection) : null;
        }

        return null;
    }

    private static IEnumerable<IGxObject> AsEnumerable(IEnumGxObject enumGxObject)
    {
        if (enumGxObject == null) throw new ArgumentNullException("enumGxObject");
        enumGxObject.Reset();

        IGxObject gxObject;
        while ((gxObject = enumGxObject.Next()) != null)
        {
            yield return gxObject;
        }
    }
share|improve this answer
    
+1 I completely forgot about the internal catalog. –  Kirk Kuykendall Dec 21 '11 at 23:22
    
is there a way to split the points between two answers? @KirkKuykendall was right on the spot, but Petr Krebs killed it. Thanks to both of you guys! How did learn to do that? Where is the docs that you guys are using? :P –  George Dec 22 '11 at 16:07

The doc for IGxDialog.StartingLocation says:

The dialog's starting location. This can be an IGxObject or a text-string containing the full name of an object.

I would interpret that to mean you could create an IWorkspaceName using your propertyset, then create a new GxDatabase object and set IGxDatabase.Workspacename to the workspacename, and finally assign the IGxDatabase to IGxDialog.StartingLocation.

share|improve this answer
    
oooh, kirk knows all. I have little experience with the gx classes and did not know the proper way to do it. I'll try it. thanks kirk! –  George Dec 21 '11 at 17:55
    
This approach, compiles and runs, but the dialog does not honor the starting location configuration. –  George Dec 21 '11 at 19:31
    
I've also tried setting this as a GxObject, but does not work also. –  George Dec 21 '11 at 20:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.