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?

user3596113
  • 868
  • 14
  • 32

1 Answers1

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;
              }
          }
      }
  }
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • You can use SendKeys to go to edit mode. See my edit. – Simon Mourier Jul 04 '14 at 13:44
  • Haven't seen such a sweet and useful extension method for a while now. Simply awesome. Couldn't have thought this way. Thanks! – RBT May 04 '16 at 10:10
  • here selected griditem background color is shown as gray. can we change it to default light blue or remove the babk color???? – Piyush Jul 04 '17 at 18:40
  • @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. – Simon Mourier Jul 04 '17 at 20:53
  • This works like a charm for me. I had to use "SendKeys.Send" instead of "SendKeys.SendWait" though. – miasbeck Nov 23 '20 at 06:35