Reputation: 2417
I define item in propertygrid. My code is here :
[DisplayName("Title of book"), CategoryAttribute("Books")]
public string BookName{ get; set; }
With this code , I can get Labele and value of this item from propertygrid(for this example : Title of book and value of it (for example) Learning C#):
private void button1_Click(object sender, EventArgs e)
{
GridItem gi = propertyGrid1.SelectedGridItem;
while (gi.Parent != null)
{
gi = gi.Parent;
}
foreach (GridItem item in gi.GridItems)
{
ParseGridItems(item); //recursive
}
}
private void ParseGridItems(GridItem gi)
{
if (gi.GridItemType == GridItemType.Category)
{
foreach (GridItem item in gi.GridItems)
{
ParseGridItems(item);
}
}
textBox1.Text += "Lable : "+gi.Label + "\r\n";
if(gi.Value != null)
textBox1.Text += "Value : " + gi.Value.ToString() + "\r\n";
}
How can I get the item name ? for this example : BookName
Upvotes: 2
Views: 5334
Reputation: 116
You can get all the entries if you inherit the PropertyGrid control. In the inherrited class, put this new property:
public GridItem[] Items
{
get
{
try
{
System.Reflection.FieldInfo field_gridView = typeof(PropertyGrid).GetField("gridView", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
object gridView = field_gridView.GetValue(this);
System.Reflection.MethodInfo gridView_method_GetAllGridEntries = gridView.GetType().GetMethod("GetAllGridEntries", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, new Type[0], null);
object gridEntries_collection = gridView_method_GetAllGridEntries.Invoke(gridView, new object[0]);
System.Reflection.MethodInfo gridEntries_collection_count_method = gridEntries_collection.GetType().GetMethod("get_Count");
int count_entries = (int) gridEntries_collection_count_method.Invoke(gridEntries_collection, new object[0]);
System.Reflection.MethodInfo gridEntries_collection_getItem_method = gridEntries_collection.GetType().GetMethod("get_Item", new Type[] { typeof(int) });
GridItem[] entries = new GridItem[count_entries];
for (int i = 0; i < count_entries; i++)
{
entries[i] = gridEntries_collection_getItem_method.Invoke(gridEntries_collection, new object[] { i }) as GridItem;
}
return entries;
}
catch
{
throw;
}
}
}
Hope it helps, Anben.
Upvotes: 0
Reputation: 37780
Use GridItem.PropertyDescriptor
property:
var propertyName = propertyGrid1.SelectedGridItem.PropertyDescriptor.Name;
Upvotes: 2