Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I fetch the Values from Database
and Show them in labels in windows Form Application.
I mean IF i have data in database as

Name Roll
ABCD 12
QWERT 75
XYZ 82

and I want to show them in form in labels
one after other.
I cant use any other Control to show data.
I just have to show the Data in labels only.
I think I have to create labels Dynamically.but how should i loop the Database and set the positions of each label.
Please assist any Idea.
Posted

Hello,

You should have a look at the "FlowLayoutPanel" it takes care automatically of the placing based on its FlowDirection and WrapContent settings.

Creating a label is quite simple:
Dim lbl As Label
Set lbl = New Label()
Set lbl.Text = "Some text retrieved from database"
flowLayoutPanel1.Controls.Add(lbl)

You could also have a look at the ListView component.

For the database part, I'd recommend using a DataReader. As for the query type either straight text "SELECT ColumnX FROM TableY" or a stored procedure that would return the same resultset.

Hope my comments make sense and help you solve your problem.
 
Share this answer
 
v2
You can iterate through each row of the database and then set the corresponding values to the labels. You can generate the labels on the run-time and can set the locations. I have shown it in the following example:

C#
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeProject
{
    public partial class Form1 : Form
    {

        Label rollNo_Label;
        Label name_Label;

        public Form1()
        {
            InitializeComponent();
        }

        int y = 31;

        private void addDymanicLabels()
        {
            for (int i = 0; i < 10; i++)
            {

                this.rollNo_Label = new Label();
                this.rollNo_Label.Text = "Roll #: " + i;
                this.labelContainers_Panel.Controls.Add(this.rollNo_Label);
                this.rollNo_Label.Location = new System.Drawing.Point(107, 36 + y);

                this.name_Label = new Label();
                this.name_Label.Text = "Name: "+i;
                this.labelContainers_Panel.Controls.Add(this.name_Label);
                this.name_Label.Location = new System.Drawing.Point(45, 36+y);

                y = y + 32;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            addDymanicLabels();
        }
    }
}
 
Share this answer
 

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