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
|
private void button1_Click(object sender, EventArgs e)
{
DataTable dt1 = CreateDataTable("bread");
// 昇順で取得します
DataTable dt2 = GetSortedDataTable(dt1, "price ASC");
// 降順で取得します
DataTable dt3 = GetSortedDataTable(dt1, "price DESC");
}
private DataTable CreateDataTable(string name)
{
DataTable dt = new DataTable(name);
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("price", typeof(int));
string[] names = { "あんぱん", "メロンパン", "カレーパン", "いちごジャムパン" };
int[] prices = { 100, 110, 98, 103 };
for (int i = 0; i < 4; i++)
{
DataRow add = dt.NewRow();
add["name"] = names[i];
add["price"] = prices[i];
dt.Rows.Add(add);
}
return dt;
}
|