How to set selected item of property grid
Asked Answered
K

1

6

I need to set the selected item of my property grid. I'm getting an eventargs, which stores a string (this string tells me what property in my propertygrid the user wants to select). The problem is i cannot find a collection of grid items, i can select one from. And also i dont know how to properly create a new GridItem object and set the SelectedGridItem property.

GridItem gridItem = ???;
detailsPropertyGrid.SelectedGridItem = gridItem;

thank you for your help.

Edit:

Its almost working now tahnk you VERY much.

GridItem gi = this.detailsPropertyGrid.EnumerateAllItems().First((item) =>
                item.PropertyDescriptor != null &&
                item.PropertyDescriptor.Name == colName);
        this.detailsPropertyGrid.SelectedGridItem = gi;
        this.detailsPropertyGrid.Select();

The only problem is: Now its selecting the Property Name field. Can I set the focus to the input field of the property?

Kauai answered 4/7, 2014 at 10:6 Comment(0)
A
14

Here are a couple of PropertyGrid extensions that can enumerate all items in a grid. This is how you can use it to select one item:

  // get GridItem for a property named "Test"
  GridItem gi = propertyGrid1.EnumerateAllItems().First((item) =>
                    item.PropertyDescriptor != null &&
                    item.PropertyDescriptor.Name == "Test");

  // select it
  propertyGrid1.Focus();
  gi.Select();

  // enter edit mode
  SendKeys.SendWait("{F4}");

  ...

  public static class PropertyGridExtensions
  {
      public static IEnumerable<GridItem> EnumerateAllItems(this PropertyGrid grid)
      {
          if (grid == null)
              yield break;

          // get to root item
          GridItem start = grid.SelectedGridItem;
          while (start.Parent != null)
          {
              start = start.Parent;
          }

          foreach (GridItem item in start.EnumerateAllItems())
          {
              yield return item;
          }
      }

      public static IEnumerable<GridItem> EnumerateAllItems(this GridItem item)
      {
          if (item == null)
              yield break;

          yield return item;
          foreach (GridItem child in item.GridItems)
          {
              foreach (GridItem gc in child.EnumerateAllItems())
              {
                  yield return gc;
              }
          }
      }
  }
Abcoulomb answered 4/7, 2014 at 12:8 Comment(5)
You can use SendKeys to go to edit mode. See my edit.Abcoulomb
Haven't seen such a sweet and useful extension method for a while now. Simply awesome. Couldn't have thought this way. Thanks!Narrows
here selected griditem background color is shown as gray. can we change it to default light blue or remove the babk color????Molokai
@picnic4u - Winforms' property grid is a very hardcoded thing, that kinda follows Windows settings, but not always, so problably no, but you should ask another question.Abcoulomb
This works like a charm for me. I had to use "SendKeys.Send" instead of "SendKeys.SendWait" though.Melo

© 2022 - 2024 — McMap. All rights reserved.