LoginSignup
0
0

cscの作法 その337

Posted at

概要

cscの作法、調べてみた。
練習問題、やってみた。

練習問題

sqliteでlistviewに流し込め。

写真

image.png

サンプルコード

using System;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Transactions;
using System.Data.SQLite;

namespace App
{
	public partial class Form1: Form {
		private Label label;
		private ListView lsv;
		public Form1() {
			Text = "listview test";
			ClientSize = new Size(310, 300);
			label = new Label();
			label.Text = "koko";
			label.Font = new Font("Geneva", 12, FontStyle.Regular);
			label.Height = 20;
			label.Width = 190;
			label.Left = 50;
			label.Top = 10;
			this.Controls.Add(label);
			lsv = new ListView();
			lsv.Location = new Point(0, 40);
			lsv.Size = new System.Drawing.Size(300, 200);
			lsv.View = View.Details;
			lsv.FullRowSelect = true;
			lsv.GridLines = true;
			lsv.Columns.Add("id", 70, HorizontalAlignment.Right);
			lsv.Columns.Add("name", 100, HorizontalAlignment.Left);
			lsv.Columns.Add("number", 50, HorizontalAlignment.Right);
			lsv.Columns.Add("price", 50, HorizontalAlignment.Right);
			lsv.CheckBoxes = true;
			//lsv.ColumnClick += lsv_ColumnClick;
			lsv.ColumnClick += new ColumnClickEventHandler(lsv_ColumnClick);
			Controls.Add(lsv);
			SQLiteConnectionStringBuilder ConnectionStr = new SQLiteConnectionStringBuilder();
			ConnectionStr.DataSource = "db0.sqlite";
			using (SQLiteConnection Connection = new SQLiteConnection(ConnectionStr.ToString()))
			{
				Connection.Open();
				using(SQLiteCommand Command = new SQLiteCommand(Connection))
				{
					Command.CommandText = "SELECT * FROM fruit ORDER BY id ASC";
					using (SQLiteDataReader Reader = Command.ExecuteReader())
					{
						while (Reader.Read())
						{
							string Id = Reader.GetInt32(0).ToString();
							string Name = Reader.GetString(1);
							string Number = Reader.GetInt32(2).ToString();
							string Price = Reader.GetInt32(3).ToString();
							lsv.Items.Add(new ListViewItem(new string[]{Id, Name, Number, Price}));
						}
					}
				}
			}
		}
		void lsv_ColumnClick(object sender, ColumnClickEventArgs e) {
			Console.WriteLine(e.Column);
			foreach (ListViewItem item in lsv.CheckedItems)
			{
				Console.WriteLine(item.SubItems[1].Text + "\r\n");
			}
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0