Home

Introduction to Visual Data Selection

   

Overview

This is an introduction to selecting records from a table (or view) and displaying them to a user.

Practical LearningPractical Learning: Introducing Data Selection

  1. Start Microsoft Visual Studio
  2. Start a new project and name it ivds

    New Project

  3. Click OK
  4. Add a list view and design the form as follows:
     

    Form Design

  5. Double-click an empty area of the form and implement the event as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace ivds
    {
        public partial class LambdaSquare : Form
        {
            public LambdaSquare()
            {
                InitializeComponent();
            }
    
            private void LambdaSquare_Load(object sender, EventArgs e)
            {
                using (SqlConnection scApartments = new SqlConnection("Data Source=CENTRAL;" +
                                                                      "Database=LambdaSquare1;" +
                                                                      "Integrated Security=Yes"))
                {
                    SqlCommand cmdApartments = new SqlCommand("SELECT * FROM Presentation.Units;", scApartments);
                    scApartments.Open();
    
                    SqlDataAdapter sdaApartments = new SqlDataAdapter(cmdApartments);
                    DataSet dsApartments = new DataSet("ApartmentsSet");
                    sdaApartments.Fill(dsApartments);
    
                    for (int i = 0; i < dsApartments.Tables[0].Rows.Count; i++)
                    {
                        DataRow drApartment = dsApartments.Tables[0].Rows[i];
    
                        ListViewItem lviApartment = new ListViewItem((i + 1).ToString());
                        lviApartment.SubItems.Add(drApartment["UnitNumber"].ToString());
                        lviApartment.SubItems.Add(drApartment["Bedrooms"].ToString());
                        lviApartment.SubItems.Add(drApartment["Bathrooms"].ToString());
                        lviApartment.SubItems.Add(drApartment["Price"].ToString());
                        lviApartment.SubItems.Add(drApartment["Deposit"].ToString());
                        lviApartment.SubItems.Add(drApartment["Available"].ToString());
    
                        lvwApartments.Items.Add(lviApartment);
                    }
                }
            }
        }
    }
  6. Execute the application (by press Ctrl + F5)
     

    Form Design


Home Copyright © 2013 FunctionX