Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,

Could you please help me with the code to create dynamic controls through sqlserver in C# windows environment.


Have to use controls for eg:combobox as table type or column in sqlserver and generate in the windows forms.


Regards
Hariharan.S
Posted
Updated 26-Aug-11 1:02am
v2
Comments
Herman<T>.Instance 26-Aug-11 5:09am    
this is want I want to develop too.
harish101 26-Aug-11 5:27am    
you tried?
Herman<T>.Instance 26-Aug-11 5:34am    
yeah but I did get to the point how to create an object from a stringvalue.
I must be something with MethodName<T> to do this
Corporal Agarn 26-Aug-11 7:03am    
Added C# tag
[no name] 26-Aug-11 7:52am    
This isn't quite clear to me. Could you give an example of the usage you expect?

1 solution

Your question doesn't exactly spell out what you want to do. Are you saying you want to create controls as specified from a SQL query? If so, you would have to do something like this:

C#
// assuming you've already retrieved your sql data into a DataTable object:
bool haveCtrl = true;
DataRow row = datatable.Rows[0];
switch (row["ControleType"].Value.ToLower())
{
    case "combobox" :
        {
            ComboBox ctrl = new ComboBox();
            ctrl.Name     = row["CtrlID"].Value;
            ctrl.Location = new Location(Convert.ToInt32(row["CtrlXPos"].Value), Convert.ToInt32(row["CtrlYPos"].Value));
            ctrl.Width    = Convert.ToInt(row["CtrlWidth"].Value);
            ctrl.Height   = Convert.ToInt(row["CtrlHeight"].Value);
            //... continue setting properties specified by the data retrieved (or static values)
        }
        break;

    case "textbox" :
        {
            // ... blah blah blah
        }
        break;
    default :
        haveCtrl = false;
        break;
}
this.Controls.Add(ctrl);


Are you really sure you want to do that? If you're adding controls based on the data you're going to be using, I would create an appropriate number of UserControl objects and just add the one that is appropriate for the data that's going to be manipulated.
 
Share this answer
 
Comments
harish101 31-Aug-11 10:37am    
thanks i will try in project..
Really i will get some solution with this code..
#realJSOP 31-Aug-11 11:25am    
Did you 1-vote the answer? If so, why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900