Sometimes you need to have a UserControl in a DataGrid, and sometime you also need to reference that control to either get/set its values. In ASP.Net, this is straight forward.
First let's create a UserControl and host Aspx page. The important sections are below, but you can download it here.
Host Aspx Page
Get the UserControl
We can reference the control absolutely:
UCSimple uc = (UCSimple)this.DataGrid1.Items[intRow].Cells[_UCColumn].Controls[_UCControlPosition];
or from the argument in an individual row.
UCSimple uc = (UCSimple)e.Item.Cells[_UCColumn].Controls[_UCControlPosition];
Note what's going on:
- Get the row (via .Items[intRow] or just e.Item)
- Then get the column (via .Cells[_UCColumn] )
- Then get the control within that cell - a single cell can have multiple controls.
- Then cast that as the UserControl type that you need.
- Then reference the get/set of any public property.
This will allow you to reference user controls from either outside the grid, or with respect to any row.