Dynamically Loaded Windows Form

I have to create a form that allows the user to interact with a DB. The tricky part is that the DB fields are subject to change at any time. I have an XML file that contains details about the fields in the DB. I need to use the information in the XML file to build a form everytime the form is loaded. The components I need to be able to load are: Label, Listbox, Combobox, Checkbox, Textbox, and Date Selector.

Also, can I make this dynamic form a scrollable subform. If this is possible I don’t have to worry about positioning the components; I could just list them vertically and allow the user to scroll through them on a main form that contains all static data.
Who is Participating?
I develop something like that . The I did was simple Transform the XML into a datatable
and in the form create a panel.
for each row in the datatable is a control, and some columns have the desired information to create that control

            public static DataTable dtDataTablefromXML(string xmlStr )
                  DataTable tb2 = new DataTable();
            try {      
                  System.IO.StringReader strM = new System.IO.StringReader(xmlStr);
                  DataSet ds2 = new DataSet();
                  tb2 = ds2.Tables[0];
                  return tb2;
            catch (Exception e) {
                  return tb2;

            public static void BuildPdaForm()
                  DataTable dt = dtDataTablefromXML(@"c:\fields.xml");
                  //when creating forms clean panel first
                  //get the reference to the panel in some way
                  Panel panel = this.pnlFrm;
                  System.Windows.Forms.Label lblTmp;
                  Control ctrlTmp;
                  int iLeft = 2 ;
                  int iSpc = 2 ;
                  int iTop = 2 ;
                  int Ac = 0;
                  foreach(DataRow row in dt.Rows) {
                        if (row["ControlType"] == "LABEL") {
                              lblTmp = new Label();
                              lblTmp.Name = row["ControlName"].ToString();
                              lblTmp.Text = row["ControlValue"].ToString();
                              lblTmp.Width = 120;
                              lblTmp.Top = iTop;
                              lblTmp.Left= iLeft;
                              //this part you calculate as want to displace the controls in the form
                              //this line would place the controls like in a single column
                              iTop = iTop + lblTmp.Top + lblTmp.Height + iSpc;
                        } else if (row["ControlType"] == "TEXTBOX") {
                              ctrlTmp = new System.Windows.Forms.TextBox();                        

                              ctrlTmp.Name = row["ControlName"].ToString();
                              ctrlTmp.Text = row["ControlValue"].ToString();
                              ctrlTmp.Width = 120;
                              ctrlTmp.Top = iTop;
                              ctrlTmp.Left = iLeft;
                              iTop = iTop + lblTmp.Top + lblTmp.Height + iSpc;
                        } //a if for every type you want the process is alike, you mike just want to check
                          //the properties names of the other controls
rufassaAuthor Commented:
I have figured out how to create the subform. So I only need an answer to the first question.
wot do you want actually, to read the xml file or to connect to db or to load a windows form and populate data in it??
i think what you want is to read the xml file. so here it goes.

whenever the form is loaded just read the xml file and populate ur contols with the relevant data.
for reading the file..
xmldocument doc = new xmldocument;
xmlnode root = doc.documentelement;

now access elements using root node.

Ok there's some english errors and some mistakes in the above comment, but way is explicit, simply have the necessary information available like the control name and whatever other properties you want to set , like field, and create the control
and add it to a panel in the way you want.
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.