- - PR -
CollecionBaseを継承したクラスをDataGridにバインド
1
投稿者 | 投稿内容 | ||||
---|---|---|---|---|---|
|
投稿日時: 2005-07-07 19:00
こんにちは
WindowsアプリケーションのDataGridに CollecionBaseを継承したクラスをデータソースとして バインドしたいのですが、実際に動かしてみると データが表示されません。(件数分の行は出来ます) データが表示されるためにはどのように設定したらよいのでしょうか? ソースを以下に記述します。 ご教授よろしくお願いします。 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace DataGrid { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { MyCollection mycoll; private System.Windows.Forms.DataGrid dataGrid1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); CreateCollection(); CreateDataGrid(); dataGrid1.AllowSorting = true; dataGrid1.SetDataBinding(mycoll, ""); } /// <summary> /// /// </summary> public void CreateDataGrid() { //Create a DataTable style object DataGridTableStyle ts1 = new DataGridTableStyle(); ts1.MappingName = "tableStyle1"; ts1.AlternatingBackColor = Color.LightBlue; // Column 1 DataGridColumnStyle boolCol = new DataGridBoolColumn(); boolCol.MappingName = "m_bool_1"; boolCol.HeaderText = "Check It"; boolCol.Width = 70; ts1.GridColumnStyles.Add(boolCol); // Column 2 DataGridColumnStyle TextCol = new DataGridTextBoxColumn(); TextCol.MappingName = "m_string_2"; TextCol.HeaderText = "String 2"; TextCol.Width = 100; ts1.GridColumnStyles.Add(TextCol); // Column 3 TextCol = new DataGridTextBoxColumn(); TextCol.MappingName = "m_string_4"; TextCol.HeaderText = "String 4"; TextCol.Width = 100; ts1.GridColumnStyles.Add(TextCol); // Add ColumnStyle to DataGrid dataGrid1.TableStyles.Add(ts1); } /// <summary> /// /// </summary> public void CreateCollection() { mycoll = new MyCollection(); mycoll.Add(new Class1(false, "s1[0]", "s2[0]", "s3[0]", "s4[0]")); mycoll.Add(new Class1(true, "s1[1]", "s2[1]", "s3[1]", "s4[1]")); mycoll.Add(new Class1(false, "s1[2]", "s2[2]", "s3[2]", "s4[2]")); mycoll.Add(new Class1(true, "s1[3]", "s2[3]", "s3[3]", "s4[3]")); mycoll.Add(new Class1(false, "s1[4]", "s2[4]", "s3[4]", "s4[4]")); mycoll.Add(new Class1(true, "s1[5]", "s2[5]", "s3[5]", "s4[5]")); mycoll.Add(new Class1(false, "s1[6]", "s2[6]", "s3[6]", "s4[6]")); mycoll.Add(new Class1(true, "s1[7]", "s2[7]", "s3[7]", "s4[7]")); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(608, 374); this.dataGrid1.TabIndex = 0; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(608, 374); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1}); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } } /// <summary> /// /// </summary> public class MyCollection : CollectionBase { public int Add(Class1 c) { return base.InnerList.Add(c); } public Class1 this[int idx] { get { return (Class1)base.InnerList[idx]; } set { base.InnerList[idx] = value; } } } /// <summary> /// Summary description for Class1. /// </summary> public class Class1 { public bool m_bool_1; public string m_string_1; public string m_string_2; public string m_string_3; public string m_string_4; public Class1(bool b, string s1, string s2, string s3, string s4) { m_bool_1 = b; m_string_1 = s1; m_string_2 = s2; m_string_3 = s3; m_string_4 = s4; } } } | ||||
|
投稿日時: 2005-07-07 19:10
Class1のフィールドをプロパティに変更してください。「フィールド」と「プロパティ」はまったく違うものです。
| ||||
|
投稿日時: 2005-07-07 19:16
#相変わらず回答ではありません。 DataGridの使い方とか、サンプルはないか調べられましたか? | ||||
|
投稿日時: 2005-07-07 21:43
こんばんは。
#同じく回答に非ず。
もう、パッと見ただけでお腹一杯、ゲップが出そうになるのは私だけでしょうか…。 せめて、現象を再現する必要最小限のコードを掲載して頂けませんか? まあ、そうやって絞り込んでいく事によって原因が特定できて、自前で解決しちゃうかも知れませんし。 | ||||
|
投稿日時: 2005-07-08 09:14
ya様 餅宮餅喜様 きくちゃん様
ご返答ありがとうございました。 ya様のおっしゃるとおりプロパティに変更したところ データを表示することができました。 今後は質問の要点をまとめてから書き込むよう気をつけます。 |
1