0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cscの作法 その331

Last updated at Posted at 2023-06-03

概要

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

練習問題

sqliteでdatagridviewでsortボタンを実装せよ。

写真

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;

class form1: Form {
	private SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter();
	private DataGridView gd0 = new DataGridView();
	private Button rButton = new Button();
	private Button sButton = new Button();
	form1() {
		Text = "DataGridView";
		ClientSize = new Size(600, 400);
		rButton.Text = "sort1";
		rButton.Location = new Point(50, 20);
		sButton.Text = "sort2";
		sButton.Location = new Point(250, 20);
		gd0.Location = new Point(50, 60);
		gd0.Size = new Size(500, 300);
		rButton.Click += new EventHandler(RButton_Click);
		sButton.Click += new EventHandler(SButton_Click);
		Controls.AddRange(new Control[] {
			rButton,
			sButton,
			gd0
		});
		SQLiteConnection conn = new SQLiteConnection("Data Source=db0.sqlite;");
		conn.Open();
		SQLiteCommand command = conn.CreateCommand();
		command.CommandText = "SELECT * FROM fruit";
		dataAdapter = new SQLiteDataAdapter(command);
		DataTable dt = new DataTable();
		dataAdapter.Fill(dt);
		gd0.DataSource = dt;
		conn.Close();
	}
	private void RButton_Click(object sender, EventArgs e) {
		DataTable dt = (DataTable) gd0.DataSource;
		DataView dv = dt.DefaultView;
		dv.Sort = "name DESC";
	}
	private void SButton_Click(object sender, EventArgs e) {
		DataTable dt = (DataTable) gd0.DataSource;
		DataView dv = dt.DefaultView;
		dv.Sort = "name ASC";
	}
	[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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?