1 Replies - 1166 Views - Last Post: 16 December 2010 - 08:18 AM Rate Topic: -----

#1 lapeci_10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

How to call database records one-by-one and display it in ListView?

Posted 16 December 2010 - 07:55 AM

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


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

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)
        {

        }
 .
 .
 .
}



If anyone can help me, it would mean the world to me...

Thanks in advance

Kind Regards

Agron

This post has been edited by insertAlias: 16 December 2010 - 08:12 AM
Reason for edit:: added [CODE] tags


Is This A Good Question/Topic? 0
  • +

Replies To: How to call database records one-by-one and display it in ListView?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,719
  • Joined: 08-June 10

Re: How to call database records one-by-one and display it in ListView?

Posted 16 December 2010 - 08:18 AM

If you're dead set on this idea, you can do the query in the constructor of the User Control. Then you could save the result to a List or an array, and then grab the proper index with each button.

However, this is a very poor design. Why do you need a button for each database entry? What happens when you add something to the DB? You'd have to add more code, and recompile.

There are controls like ListView and ListBox that are designed to be bound to data. They take a template and then for each item in a query they'll copy that template. That's how you should be looking to do something like this.

Also, don't keep the default names of your controls. Button1, Form2, ListView1...these names are meaningless. Control names should be self-descriptive, meaning that you should be able to look at the name of the button and have at least a rough idea of what it does.

One other thing:
:code:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1