winform form build using data-driven controls

Posted on 2006-05-31
Last Modified: 2010-04-16
I would like to design a process that will read a sql database to identify what controls go on a form for a reporting process.  The things that I see stored in the database table are the controls needed, what order they should be displayed in, the text label that should be associated with the control, and the crystal report parameter they are to be associated with as well as some other pertinent information for processing. That's what I have in mind so far.

I would then pull the list of controls in through a dataset ordered by the order specified in the table for display, loop through and dynamically position them on the screen.  I then need to gather the information entered in the control and send it with it's crystal report parameter to our crystal reports process to generate the report.

The concept being that the user can specify the parameters in a table and the screen would build itself so if they have a new report, it doesn't necessarily need coding changes.

So this is the code I have to dynamically add it but in a non-data driven way.

            CreditAcctEntry ctrlCreditAcctEntry = new CreditAcctEntry();
            ctrlCreditAcctEntry.Location = new Point(12, 126);

What I'm trying to figure out is a syntax to make ctrlCreditAcctEntry a constructed name of sorts if that makes any sense...meaning I pulled from the table that I need to put CreditAcctEntry control on the screen.  Theoretically I may have multiple of the same control with different text associated with parms...for instance, a begin credit acct and an end credit acct.  So to be completely dynamic, I need to be able to construct the name of the object (ie: ctrlCreditAcctEntry1 and ctrlCreditAcctEntry2) and then be able to reference it later, without a hardcoded name, for gathering the properties returned from it after data entry.  Or maybe there is simply a better way to go about this and get the same results.

Any help you can give me would be greatly appreciated.
Question by:babybird
    LVL 12

    Accepted Solution

    First, i suggest you to check if a simpler solution isn't convenient: You can display  a 2 column datagrid 'Parameter' and 'Value', first column will be filled with parameters name and the second column will be for entering the values required by the report.

    If that is not a choice, here's another option:

     As a starting point you should read about reflection, this is what you should use to dynamicaly create controls and assign values to their properties.
     You could use CreateInstance method of Activator class to create a control whose name is taken form a table field. Then you can use PropertyInfo, FieldInfo, or MethodInfo classes to set values for new created controls. As an example, if you have

       public class CreditAcctEntry : TextBox
        { }

      you create an instance of this type with something like:

       object obj;
       string clsName = "namespace.CreditAcctEntry";
       string prop = "Text";
       Type ctype = Type.GetType(clsName);
       obj = Activator.CreateInstance(ctype);
       ((Control)obj).Location = new Point(50, 50); // CreditAcctEntry being a control, it has 'Location', the coordinates can be read from a table
          PropertyInfo pinf = ctype.GetProperty(prop);  // this is for properties new to CreditAcctEntry class, prop can be another field name
          this.Controls.Add(obj as Control);

        Those 'obj' objects can be kept in an array of objects, in order to have acces to their properties in the end.

    Author Comment

    I'm curious about the datagrid option.... so with the custom controls we need for some of the fields, I could place the specific controls in the datagrid?  This would mean that I would not have to concern myself with positioning and such, correct?  What other benefits would there be?  If I'm asking poor questions, I apologize as I'm new to

    One other thought I had were situations where there are dependent controls meaning I enter a customer and the second control is populated with data directly related to that customer and they would select from a list.  My thought for this situation was to make a control that consists of both fields.  Does this make the datagrid option, in your mind, still a viable solution?

    LVL 12

    Expert Comment


     No, you cannot have in the same column of a datagrid different controls on different lines. If you have situations like: depending on a text entered in a textbox, fill a combobox with specific items (or set a specific datasource for it) you'll need the second solution.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now