I'm looking for a way to get a coded domain list based on it's name so that I can use on as combo box items in a custom dockpane or control. Am I missing the easy way to do this? There doesn't seem to be a way to do this from a workspace. There is a sample that shows how to query them directly from gdb_items in the database. I've also been able to access them through a feature but that isn't a very direct method. I'm currently hard coding the values and descriptions as shown in the image. I would rather not do that for obvious reasons.
Solved! Go to Solution.
Hi Jimmy,
You can get a list of the domains in a geodatabase with the Geodatabase.GetDomains method. This returns an IReadOnlyList which you can search using a Linq query.
IReadOnlyList<Domain> domains = myGeodatabase.GetDomains();
Domain myDomain = domains.First(x => x.GetName() == myDomainName);
I hope this helps,
--Rich
Hi Jimmy,
You can get a list of the domains in a geodatabase with the Geodatabase.GetDomains method. This returns an IReadOnlyList which you can search using a Linq query.
IReadOnlyList<Domain> domains = myGeodatabase.GetDomains();
Domain myDomain = domains.First(x => x.GetName() == myDomainName);
I hope this helps,
--Rich
Wow I'm feeling very dense at the moment. I thought I looked on the geodatabase object, I guess I just missed it. Thanks for the prompt response @RichRuh
Hi Rich,
How can the domains be accessed if the layers are loaded from a feature server and do not exist in a geodatabase? I searched the SDK guide here but could not find any method that can do this.
Hi Karthik,
You can create a Geodatabase object from a feature service by creating a ServiceConnectionProperties object. You can then instantiate a Geodatabase using the Geodatabase(ServiceConnectionProperties) constructor.
Once you have a Geodatabase you can proceed as described above.
I hope this helps,
--Rich
Thank you, Rich. That worked!
In case someone stumbles on this post and needs a bit more here's an extension method to return the values of an Integer based coded value domain from a geodatabase as a dictionary.
public static Dictionary<int, string> GetDomainValuesInt(this Geodatabase geodatabase, string name)
{
Dictionary<int, string> retval = new Dictionary<int, string>();
IReadOnlyList<Domain> domains = geodatabase.GetDomains();
Domain namedomain = domains.FirstOrDefault(a => a.GetName().Equals(name, StringComparison.CurrentCultureIgnoreCase));
if (namedomain != null && namedomain is CodedValueDomain)
{
//need to cast the domain to CodedValueDomain to get the Value Pairs
CodedValueDomain cvd = namedomain as CodedValueDomain;
SortedList<object, string> valuePairs = cvd.GetCodedValuePairs();
foreach (var item in valuePairs)
{
if (int.TryParse(item.Key.ToString(), out int i))
{
retval.Add(i, item.Value);
}
}
}
return retval;
}