概要
cscの作法、調べてみた。
練習問題、やってみた。
練習問題
sqliteでdatagridviewでsortボタンを実装せよ。
写真
サンプルコード
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());
}
}
以上。