如何获得一个PropertyGrid';s单元值(c#)?

如何获得一个PropertyGrid';s单元值(c#)?,c#,propertygrid,C#,Propertygrid,如何在c#中获取属性网格项和项的值? 例如: Name : Ali LastName : Ahmadi (Name和LastName是propertygrid的两个属性)propertygrid只是对象的组件模型表示的视图。与其看网格,不如说:看组件模型,例如: var props = TypeDescriptor.GetProperties(obj); foreach(var prop in props) { string name = prop.DisplayName; i

如何在c#中获取属性网格项和项的值? 例如:

Name : Ali
LastName : Ahmadi

(Name和LastName是propertygrid的两个属性)

propertygrid
只是对象的组件模型表示的视图。与其看网格,不如说:看组件模型,例如:

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));
}
正确答案是:

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";
}