Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.50/5 (4 votes)
See more:
i want is that, when i fetch data from database then labels dynamically added to my form.

i have a code but this show only one data, i want total result of query generate automatically label and set data into label.

my code is .

`
C#
con.Open();
               DataTable table = new DataTable();
               string sqlText = "SELECT DATEDIFF(DAY, [meas_duedate], GETDATE()) , meas_id, client_id FROM tlb_meas_master"
               SqlCommand cmd = new SqlCommand(sqlText, con);

               SqlDataReader dr = cmd.ExecuteReader();
               while (dr.Read())
               {

                   }
                   lbl_date.Text = dr.GetValue(0).ToString();
                   lbl_mea_id.Text = dr.GetValue(1).ToString();
                   lbl_cli_id.Text = dr.GetValue(2).ToString();

               }
               con.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message + "\n" + ex.Message);
               con.Close();
           }

`please help me....
Posted
Updated 16-Dec-15 12:52pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Dec-15 17:15pm    
C?!
—SA
Member 10484162 16-Dec-15 17:32pm    
? you understand my question ?
Sergey Alexandrovich Kryukov 16-Dec-15 17:49pm    
You tagged the question as "C". What are you talking about?
If this is C#, the problem is trivial. What is your lbl_* objects. Are they already created (constructed)? If not, create and insert them. What's the problem?
—SA
Member 10484162 16-Dec-15 17:53pm    
yes this is c# winforms, i want when my sql query run then winform create dynamically label and value set from database. sory for my bad english hope u understand my problem
Member 10484162 16-Dec-15 17:56pm    
c# winform, i have a application and in application i have a notification panel, i want when user login then notification panel show all record of this user. i use datagridview but this is not suitable, i want a label to show data, there is many datain my database so please help me how to create dynamically label and how to populate data into dynamically created labels.

You need to add the created Label to a Form or other ContainerControl's ControlCollection to make it 'show:
LabelPanel.Controls.Add(newLabel);
And, you'll need to set the Label's position/location to 'show it where you want it to appear in its container:
newLabel.Location = new Point(100,200);
If you set the Dock Property of the Label, where it appears in its ContainerControl will be dependent on the 'DockStyle settig ... and what other Controls are "in" the host Control's ControlCollection that are also docked:
newLabel.Dock = DockStyle.Top;
 
Share this answer
 
v2
"Create label dynamically" simply means
C#
Label myLabel = new Label();
// then you can assign Text, Left, Top, other properties

Then you need to add a label to some parent control:
C#
myLabel.Parent = parent;
// the same as
parent.Controls.Add(myLabel);

And so on…

—SA
 
Share this answer
 
Comments
Member 10484162 17-Dec-15 15:51pm    
thanks sir, but i want all data show in label, for example sql query have a four result then winform create 4 label dynamically and set column value of sqlserver. hope u understand sir, and sorry for my bad languange.
Sergey Alexandrovich Kryukov 17-Dec-15 15:54pm    
Didn't I answer your question already? You asked about adding a label dynamically. Remaining step is assigning some string values from your data to the Text property, myLabel.Text = ...
—SA

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