0

I've got a GridView that shows up in my Locals window. The path I take is:

This -->   
  DataGrid_AA -->
    Controls -->
      Results View -->
        [0] -->
          Controls -->
            Results View -->
              [0] -->    
                Cells -->
                  Results View -->
                    [2] --> X

How would I go about referencing "X" in code? I tried:

string x = this.DataGrid_AA.Controls[0].Controls[0].Cells[2].Text;

but it tells me "Control does not contain a definition for Cells". Any ideas?

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
  • In WPF we have [`VisualTreeHelper`](http://stackoverflow.com/a/978352/1997232), in winforms you have to implement it yourself (check type to see what property contains children, repeat recursively until needed control is found by some criteria). – Sinatr Mar 13 '17 at 15:44
  • I can't actually test as I don't have time to set up the grid with the correct control hierarchy... but try var myTargetControl = this.dataGridView1.Controls.Find("ControlName", true); The "true" says search all children... I'm sure I used this to grab a drop down once on a grid.. – Wheels73 Mar 13 '17 at 15:50
  • If you know for sure what type `this.DataGrid_AA.Controls[0].Controls[0]` is, then simply cast (pseudo-code): `((DataGrid)this.DataGrid_AA.Controls[0].Controls[0]).Cells...` – Sinatr Mar 13 '17 at 15:59

0 Answers0