How do I reference controls in in TabPage in C#

How do I reference controls that are in a TabPage of a TabControl?

 I have a TabControl called tabMain, and have created three TabPages called tabHazard,tabRisk,tabAction. Inside tabHazard I have a datagrid called dgridHazard. How would I reference this datagrid in code?

I thought it might be something like tabMain.tabHazard.dgridHazard but I'm not getting any joy.
Who is Participating?
denbessonovConnect With a Mentor Commented:
You can refer directly to dgridHazard. There is no need to specify tab, because dgridHazard is the child component of your form's class..
chriscboyAuthor Commented:
Lol I just discovered that as your message arrived!! I don't understand this because I thought the dgridhazard would be a child object of the TabControl and not the main form.
Umar Topia.Net Full Stack DeveloperCommented:
Try this
DataGrid dgid = tabMain.Controls[0].FindControl("dgridHazard") as DataGrid;

if it does not work then 

DataGrid dgid = tabMain.Items[0].FindControl("dgridHazard") as DataGrid;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.