1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace dvw
{
public partial class dvw001 : Form
{
public dvw001()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = CreateDataTable();
DataView dv = new DataView(dt);
// 値段は100~150で
// 安い順でソートし同じ値段の場合はIDが大きいものを先に表示
dv.RowFilter = "100 <= Value AND Value <= 150";
dv.Sort = "Value ASC, ID DESC";
dataGridView1.DataSource = dv;
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
// 3列定義します。
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Value", typeof(int));
{
DataRow row = dt.NewRow();
row["ID"] = "0001"; row["Name"] = "あんぱん"; row["Value"] = 100;
dt.Rows.Add(row);
}
{
DataRow row = dt.NewRow();
row["ID"] = "0002"; row["Name"] = "メロンパン"; row["Value"] = 120;
dt.Rows.Add(row);
}
{
DataRow row = dt.NewRow();
row["ID"] = "0003"; row["Name"] = "カレーパン"; row["Value"] = 160;
dt.Rows.Add(row);
}
{
DataRow row = dt.NewRow();
row["ID"] = "0004"; row["Name"] = "いちごジャムパン"; row["Value"] = 155;
dt.Rows.Add(row);
}
{
DataRow row = dt.NewRow();
row["ID"] = "0005"; row["Name"] = "チョココロネ"; row["Value"] = 110;
dt.Rows.Add(row);
}
{
DataRow row = dt.NewRow();
row["ID"] = "0006"; row["Name"] = "もちふわパンケーキ"; row["Value"] = 100;
dt.Rows.Add(row);
}
return dt;
}
}
}
|