title: UI Automation not working for DataGridView
body: <p>After trying out several solutions, I am in desperate need for help.</p>
  
  <p>I tried several approaches, before finally copying and still being stuck with the solution from <a href="https://stackoverflow.com/questions/12129592/getting-full-contents-of-a-datagrid-using-uiautomation">Getting full contents of a Datagrid using UIAutomation</a>.</p>
  
  <p>Let's talk code, please consider the comments:</p>
  
  <pre><code>// Get Process ID for desired window handle
  uint processID = 0;
  GetWindowThreadProcessId(hwnd, out processID);
  
  var desktop = AutomationElement.RootElement;
  
  // Find AutomationElement for the App's window
  var bw = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, (int)processID));
  
  // Find the DataGridView in question
  var datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "dgvControlProperties"));
  
  // Find all rows from the DataGridView
  var loginLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));
  
  // Badumm Tzzz: loginLines has 0 items, foreach is therefore not executed once 
  
  foreach (AutomationElement loginLine in loginLines)
  {
      var loginLinesDetails = loginLine.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
  
      for (var i = 0; i &lt; loginLinesDetails.Count; i++)
      {
          var cacheRequest = new CacheRequest 
          { 
              AutomationElementMode = AutomationElementMode.None,
              TreeFilter = Automation.RawViewCondition
          };
  
          cacheRequest.Add(AutomationElement.NameProperty);
          cacheRequest.Add(AutomationElement.AutomationIdProperty);
  
          cacheRequest.Push();
  
          var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));
  
          cacheRequest.Pop();
  
          var myString = targetText.Cached.Name;
      }
  
  }
  </code></pre>
  
  <p>I can neither fetch a <code>GridPattern</code>, nor a <code>TablePattern</code> instance from <code>datagrid</code>, both results in an exception:</p>
  
  <pre><code>GridPattern gridPattern = null;
  
  try
  {
      gridPattern = datagrid.GetCurrentPattern(GridPattern.Pattern) as GridPattern;
  }
  catch (InvalidOperationException ex)
  {
      // It fails!
  }
  
  TablePattern tablePattern = null;
  
  try
  {
      tablePattern = datagrid.GetCurrentPattern(TablePattern.Pattern) as TablePattern;
  }
  catch (InvalidOperationException ex)
  {
      // It fails!
  }
  </code></pre>
  
  <p>The rows were added to the <code>DataGridView</code> beforehand, like this:</p>
  
  <pre><code>dgvControlProperties.Rows.Add(new object[] { false, "Some Text", "Some other text" });
  </code></pre>
  
  <p>I am compiling to .Net Framework 4.5. Tried both regular user rights and elevated admin rights for the UI Automation client, both yielded the same results described here.</p>
  
  <p>Why does the <code>DataGridView</code> return 0 rows?</p>
  
  <p>Why can't I get one of the patterns?</p>
  
  <p>Kudos for helping me out!</p>
  
  <hr>
  
  <h1>Update:</h1>
  
  <p>James help didn't to the trick for me. The following code tough returns all rows (including the headers):</p>
  
  <pre><code>var rows = dataGrid.FindAll(TreeScope.Children, PropertyCondition.TrueCondition);
  </code></pre>
  
  <p>Header cells can then be identified by their <code>ControlType</code> of <code>ControlType.Header</code>.</p>