如何从数据库中获取值并在Windows窗体应用程序中的标签中显示它们。 [英] How can I fetch the Values from Database and Show them in labels in windows Form Application.

查看:165
本文介绍了如何从数据库中获取值并在Windows窗体应用程序中的标签中显示它们。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库中获取值

并在Windows窗体应用程序中的标签中显示它们。

我的意思是如果我在数据库中有数据



名称卷

ABCD 12

QWERT 75

XYZ 82



我希望以标签形式显示它们

一个接一个。

我无法使用任何其他控制来显示数据。

我只需要在标签中显示数据。

我想我必须动态创建标签。但是我应该如何循环数据库并设置每个标签的位置。

请协助任何想法。

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.

推荐答案

你好,



你应该看看FlowLayoutPanel它根据其 FlowDirection WrapContent 设置自动处理放置。



创建标签非常简单:

Dim lbl As Label

设置lbl =新标签()

设置lbl。 Text =从数据库中检索到的一些文字

flowLayoutPanel1.Controls.Add(lbl)



您还可以查看ListView组件。



对于数据库部分,我建议使用DataReader。至于查询类型,直接文本SELECT ColumnX FROM TableY或将返回相同结果集的存储过程。



希望我的评论有意义并帮助您解决您的问题。
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.


您可以遍历数据库的每一行,然后将相应的值设置为标签。您可以在运行时生成标签,并可以设置位置。我在以下示例中显示了它:



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:

<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();
        }
    }
}


这篇关于如何从数据库中获取值并在Windows窗体应用程序中的标签中显示它们。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆