C#でList
▼外観はこんな感じで用意しておいて‥
▼ボタンを押すとこうなる感じです。
複数のList
単にMath.Maxだと2つの値しか比較できないので、LINQで検索したら出てきたこちらの記事を参考にさせて頂きました。
qiita.com
コードはこんな感じ。
// Listの中身を適当に定義 List<string> list1 = new List<string> { "a", "b" ,"c" }; List<string> list2 = new List<string> { "1", "2", "3" }; List<string> list3 = new List<string> { "A", "B", "C", "D", "E" }; List<string> list4 = new List<string> { "4", "5", "6" }; //下準備 private void Form1_Load(object sender, EventArgs e) { foreach (string s in list1) { textBox1.AppendText(s + "\r\n"); } foreach (string s in list2) { textBox2.AppendText(s + "\r\n"); } foreach (string s in list3) { textBox3.AppendText(s + "\r\n"); } foreach (string s in list4) { textBox4.AppendText(s + "\r\n"); } //行ヘッダを非表示にする ※Formデザイナーのプロパティ内でも設定可能 dataGridView1.RowHeadersVisible = false; } // ボタン押下でDataGridViewに表示 private void button1_Click(object sender, EventArgs e) { //Listの最大格納数を割り出す int iMax = new List<int> { list1.Count, list2.Count, list3.Count, list4.Count }.Max(); //Listの最大格納数だけ行を追加 for (int i = 0; i < iMax; i++) { dataGridView1.Rows.Add(); } //行にデータを入力 for (int i = 0; i < list1.Count; i++) { dataGridView1.Rows[i].Cells[0].Value = list1[i]; } for (int i = 0; i < list2.Count; i++) { dataGridView1.Rows[i].Cells[1].Value = list2[i]; } for (int i = 0; i < list3.Count; i++) { dataGridView1.Rows[i].Cells[2].Value = list3[i]; } for (int i = 0; i < list4.Count; i++) { dataGridView1.Rows[i].Cells[3].Value = list4[i]; } }