如何一一调用数据库记录并将其显示在ListView中? [英] How to call database records one-by-one and display it in ListView?
问题描述
亲爱的人们
我面临一个大问题...
我创建了一个 C#项目.在这个项目中,我有一个 Form1 (在此表单中,我有一个 Button1 , Panel1 和 ListView1 ),可重用的 UserControl1 (在该usercontrol中,我有10个按钮,例如: Button1 , Button2 , Button3 等) ..),还有一个Access 2007 数据库(在我的数据库中,我创建了一个包含10条记录的表).
到目前为止,我所做的是:
-从 Form1 中, Button1_Click 事件调用 UserControl1 并将其(在 Panel1 内部)显示在我的 Form1 .
我要实现的目标是:
- UserControl1 中要与我的数据库动态链接的10个按钮,我的意思是动态地表示我不想为每个 button_click 事件创建一个打开的数据库连接,exec查询等等,但是要创建一个并且只有一个 SQL 查询(至少像数组查询一样),所以当我单击 Button1 时,从数据库表中获取rocord1并将其显示到我的 ListView1 中,与 Button2 , Button3 相同,依此类推...
这是我的C#项目的代码:
Form1
Dear People
I''m facing a big problem...
I have created a C# Project. In this project I have a Form1 (inside this form I have a Button1, Panel1 and ListView1), a reusable UserControl1 (inside this usercontrol I have 10 buttons such as: Button1, Button2, Button3, etc, etc ..) and also I have a Access 2007 Database (In my database I have a table created with 10 records).
What I have done so far is:
- From Form1 the Button1_Click event calls UserControl1 and displays it (inside Panel1) into my Form1.
What I''m trying to achieve is this:
- The 10 buttons in UserControl1 to be linked dynamically with my Database, what I mean by dynamically is that I dont want to create for every button_click event an opening db connection, exec query, and so on, but to create one and only one SQL query (lest say like an array query) so when I click Button1 to get the rocord1 from database table and display it into my ListView1, same for Button2, Button3 and so on...
Here is the code of my C# project:
Form1
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;
using System.Windows.Controls;
using Microsoft.VisualBasic.PowerPacks;
using System.Data.OleDb;
using System.Data.Common;
using System.Windows.Forms.Integration;
public partial class DtposMDIParentSystem : Form
{
public DtposMDIParentSystem()
{
InitializeComponent();
}
private void DtposMDIParentSystem_Load(object sender, EventArgs e)
{
this.Panel1.Location = new System.Drawing.Point(332, 127);
this.Panel1.Name = "Panel1";
this.Panel1.Size = new System.Drawing.Size(700, 560);
}
UserControl1 userC = new UserControl();
//----------------------------------------------------------//
private void Button1_Click(object sender, EventArgs e)
{
this.Panel1.Visible = true;
this.userC.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.userC.Location = new System.Drawing.Point(0, 0);
this.userC.Size = new System.Drawing.Size(696, 556);
this.userC.Enabled = true;
this.userC.Visible = true;
this.Panel1.Controls.Add(userC);
}
}
Form1.Designer.cs上ListView1的声明
The declaration of ListView1 on Form1.Designer.cs
this.ListView1 = new System.Windows.Forms.ListView();
// ListView1
this.ListView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ListView1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
this.ListView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colHeaderNumber,
this.colHeaderItem,
this.colHeaderPrice});
this.ListView1.FullRowSelect = true;
this.ListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.ListView1.Location = new System.Drawing.Point(3, 5);
this.ListView1.Name = "ListView1";
this.ListView1.Size = new System.Drawing.Size(393, 300);
this.ListView1.TabIndex = 8;
this.ListView1.UseCompatibleStateImageBehavior = false;
this.ListView1.View = System.Windows.Forms.View.Details;
private System.Windows.Forms.ListView ListView1;
UserControl1
UserControl1
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;
using System.Data.OleDb;
using System.Data.Common;
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
private void Button1_Click(object sender, EventArgs e)
{
}
private void Button2_Click(object sender, EventArgs e)
{
}
private void Button3_Click(object sender, EventArgs e)
{
}
.
.
.
}
如果有人可以帮助我,那对我来说意味着世界...
在此先感谢
亲切的问候
Agron
If anyone can help me, it would mean the world to me...
Thanks in advance
Kind Regards
Agron
推荐答案
这是您第三次提出基本相同的问题.
第一个是此处 [ ^ ] 2010年10月12日,然后是几天前.
两次您都得到了对我来说似乎合理的答案.
现在,要么您是某种巨魔,要么编程确实不是您的正确工作/爱好选择.我想,您可能无法正确描述问题.
我不愿给出有帮助的答案.
This is the third time that you have asked essentially the same question.
The first one was here[^] on the 10/12/2010 and then again a couple of days ago.
Both times you got answers that seem reasonable to me.
Now either you are some kind of Troll, or programming really isn''t the right employment/hobby choice for you. There is, I suppose, the possibility that you are unable to describe your problem correctly.
Whichever it is I am disinclined to give a helpful answer.
这篇关于如何一一调用数据库记录并将其显示在ListView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!