问题
How can I get property grid items and item's value in c# ? for example :
Name : Ali
LastName : Ahmadi
(Name and LastName are 2 properties of propertygrid)
回答1:
PropertyGrid
is just a view over the component-model representation of an object. Rather than look at the grid, I would say: look at the component-model, for example:
var props = TypeDescriptor.GetProperties(obj);
foreach(var prop in props) {
string name = prop.DisplayName;
if(string.IsNullOrEmpty(name)) name = prop.Name;
Console.WriteLine("{0}: {1}", name, prop.GetValue(obj));
}
回答2:
Correct answer is :
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";
}
来源:https://stackoverflow.com/questions/12091375/how-to-get-a-propertygrids-cell-value-c