?
Solved

winform form build using data-driven controls

Posted on 2006-05-31
3
Medium Priority
?
208 Views
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);
            this.Controls.Add(ctrlCreditAcctEntry);

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.
0
Comment
Question by:babybird
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
sumix earned 2000 total points
ID: 16805216
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
      pinf.SetValue(obj,"Avalue",null);
      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.
           
0
 

Author Comment

by:babybird
ID: 16808188
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 C#.net.

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?

Thanks!
0
 
LVL 12

Expert Comment

by:sumix
ID: 16810071

 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.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Suggested Courses

621 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