C# .NETASP(VS2019) DeskTopApplication [ListView + DataReader(OFFICE365)] (2019-11)


ListViewとコンボボックス、テキストボックス、dateTimePicker配置。
dateTimePickerは未使用。

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.OleDb;
using System.Data.SqlClient;
namespace App
{
    public partial class Form1 : Form
    {

        String ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\test.accdb"; // MDB名など
        OleDbConnection conn;
        OleDbCommand comm;

        public void SetCombo()
        {
            conn = new OleDbConnection();

            conn.ConnectionString = ConnectionString;
            conn.Open();

            comm = new OleDbCommand();
            comm.CommandText = "SELECT * FROM T_COMBO";
            comm.Connection = conn;

            OleDbDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                comboBox1.Items.Add((string)reader["COMBO"]);

            }
            conn.Close();
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // ListViewコントロールのプロパティを設定
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
            //listView1.Sorting = SortOrder.Ascending;
            listView1.View = View.Details;

            // 列(Column)ヘッダの作成
            ColumnHeader[] columnHead = new ColumnHeader[4];
            for (int i = 0; i < 4; i++)  columnHead[i] = new ColumnHeader();

            columnHead[0].Text = "ID";
            columnHead[0].Width = 45;
            columnHead[1].Text = "施設名";
            columnHead[1].Width = 70;
            columnHead[2].Text = "施設担当";
            columnHead[2].Width = 90;
            columnHead[3].Text = "生年月日";
            columnHead[3].Width = 80;


            listView1.Columns.AddRange(columnHead);

            SetCombo();


        }


        private void Button1_Click(object sender, EventArgs e)
        {
            
            listView1.Items.Clear();

            conn = new OleDbConnection();

            conn.ConnectionString = ConnectionString;
            conn.Open();

            comm = new OleDbCommand();
            comm.CommandText = "SELECT * FROM T1 WHERE フィールド5 LIKE '%"+ textBox1.Text +"%'";
            comm.Connection = conn;


            OleDbDataReader reader = comm.ExecuteReader();

            string re;
            ListViewItem lvi;
            int df;

            while (reader.Read())
            {

                try
                {
                    // デシマル オートナンバー等
                    df = reader.GetInt32(0);
                    lvi = listView1.Items.Add(df.ToString());

                    re = (string)reader["フィールド7"];
                    lvi.SubItems.Add(re);

                    re = (string)reader["フィールド8"];
                    lvi.SubItems.Add(re);

                    re = (string)reader["フィールド5"];
                    lvi.SubItems.Add(re);

                }
                catch (InvalidCastException) { }

            }

            conn.Close();
            
            int itemCnt = listView1.Items.Count;
            this.Text = itemCnt.ToString() + "件";


            dateTimePicker2.Value = new DateTime(2018, 6, 1);
        }

        private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ListView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {

        }



        private void ListView1_DoubleClick(object sender, EventArgs e)
        {
            textBox1.Text = listView1.SelectedItems[0].Text;
        }

        private void ComboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            comboBox1.Text = "";
        }
         
    }
}


戻る