How to get a PropertyGrid's cell value (c#)?

十年热恋 提交于 2019-12-22 11:12:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!