###前提・実現したいこと
Visual Studio 2015, C#でWindows Formアプリケーションを作っています。
DataGridViewにソースをバインディングしているとき、列の並び順を変更するには、どうしたらよいでしょうか?
###発生している問題・エラーメッセージ
Age, Like, Name, Sex
を
Name, Sex, Age, Like,
の順にしたい。
this.dataGridView1.Columns[2].DisplayIndex = 0; this.dataGridView1.Columns[3].DisplayIndex = 1;
でできますがあまりに直接的で。データ構造が変わったらすぐ使えなくなりそうです。
あわせて列の幅を指定する方法もご教示ください。
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.Windows.Forms; 4 5namespace WindowsFormsApplication2 6{ 7 public partial class Form1 : Form 8 { 9 public Form1() 10 { 11 InitializeComponent(); 12 } 13 14 private void Form1_Load(object sender, EventArgs e) 15 { 16 List<InfoModel> models = new List<InfoModel>(); 17 var man1 = new InfoModel { 18 Name="A", 19 Sex="M", 20 Age=19, 21 Like="BascketBall" 22 }; 23 models.Add(man1); 24 var woman1 = new InfoModel 25 { 26 Name = "B", 27 Sex = "F", 28 Age = 18, 29 Like = "Dance" 30 }; 31 models.Add(woman1); 32 33 this.dataGridView1.DataSource = new BindingSource(models, string.Empty); 34 this.dataGridView1.Columns[2].DisplayIndex = 0; 35 this.dataGridView1.Columns[3].DisplayIndex = 1; 36 37 } 38 } 39 public class InfoModel : BaseModel 40 { 41 public int Age { get; set; } 42 public string Like { get; set; } 43 } 44 45 public class BaseModel 46 { 47 public string Name { get; set; } 48 public string Sex { get; set; } 49 } 50}
###補足情報(言語/FW/ツール等のバージョンなど)
Visual Studio 2015 Pro

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。