2017-01-27 2 views
1

私は1つのusercontrolを作成し、TextBoxの関数を追加しました。このusercontrolをDataGridViewColumnにバインドします。しかし、ユーザーがコントロールするカスタムプロパティにどのようにアクセスできるかわかりません。私はチュートリアルhttps://msdn.microsoft.com/en-us/library/7tas5c80.aspx?PHPSESSID=o1fb21liejulfgrptbmi9dec92カスタムDataGridViewColumnのカスタムEditingControlのプロパティを設定する方法

public class IntegerCell : DataGridViewTextBoxCell 
{ 
    //Implementations 
} 

public class IntegerColumn : DataGridViewColumn 
{ 
    public IntegerColumn() 
     :base(new IntegerCell()) 
    { 

    } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
      return base.CellTemplate; 
     } 
     set 
     { 
      // Ensure that the cell used for the template is a CalendarCell. 
      if (value != null && 
       !value.GetType().IsAssignableFrom(typeof(IntegerCell))) 
      { 
       throw new InvalidCastException("Must be a IntegerCell"); 
      } 
      base.CellTemplate = value; 
     } 
    } 

    [Browsable(true)] 
    [Category("Custom")] 
    [Description("")] 
    [DisplayName("Max. Value")] 
    public int MaxValue { get; set; } 

    [Browsable(true)] 
    [Category("Custom")] 
    [Description("")] 
    [DisplayName("Min. Value")] 
    public int MinValue { get; set; } 
} 

public partial class IntegerEditingControl : IntegerTextBox, IDataGridViewEditingControl 
{ 
    //Implementations 
} 

public class IntegerTextBox : TextBox 
{ 
    [Browsable(true)] 
    [Category("Custom")] 
    [Description("")] 
    [DisplayName("Max. Value")] 
    public int MaxValue { get; set; } 

    [Browsable(true)] 
    [Category("Custom")] 
    [Description("")] 
    [DisplayName("Min. Value")] 
    public int MinValue { get; set; } 
} 

public partial class Form1 : Form 
{ 
    private DataGridView dataGridView1 = new DataGridView(); 

    public Form1() 
    { 
     InitializeComponent(); 
     this.dataGridView1.Dock = DockStyle.Fill; 
     this.Controls.Add(this.dataGridView1); 
     this.Load += new EventHandler(Form1_Load); 
     this.Text = "DataGridView column demo"; 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     IntegerColumn col = new IntegerColumn(); 
     this.dataGridView1.Columns.Add(col); 
     this.dataGridView1.RowCount = 5; 

     int i = 0; 
     foreach (DataGridViewRow row in this.dataGridView1.Rows) 
     { 
      row.Cells[0].Value = i++; 
     } 
    } 
} 

を追った方法ベース

私は、プロパティをユーザーコントロールするためのDataGridViewカスタム列を設定した値を渡したいです。

+0

短い答えは次のようになります。オーバーライド 'InitializeEditingControl'と' DataGridView.EditingControl'を使用してコントロールを編集取得し、その値を設定します。しかし、 'DataGridView'のカスタム列を作成することについてもっと多くのことを考慮する必要があります。 –

答えて

1

問題を修正するには、コードに複数の修正を適用する必要がありますが、一般的な答えとして、カスタムセルのメソッドInitializeEditingControlは編集コントロールを設定する必要があります。編集コントロールにアクセスしてそのプロパティを設定できるようにするには、InitializeEditingControlを上書きする必要があります。

public override void InitializeEditingControl(int rowIndex, object 
    initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
{ 
    base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); 
    YourEditingControl c = DataGridView.EditingControl as YourEditingControl; 
    //Set c.Properties here 
} 

これは、コードに必要な唯一の修正ではありません。リンクされた記事は単純な例に過ぎず、他の点も考慮する必要があります。

  1. カスタムセルには、カスタム列と同じプロパティが必要です。また、編集コントロールも同じプロパティを持つ必要があります。
  2. カスタム列では、プロパティのゲッターで、CellTemplateからプロパティ値を取得する必要があります。
  3. プロパティのセッターでは、プロパティ値をCellTemplateに設定し、ループがforのループを使用して列のすべてのセルに対してプロパティ値を設定します。ユーザーの期待する列設定はすべてのセルに適用されるためです。
  4. カスタムセルでは、Cloneをオーバーライドして、カスタムセルのプロパティの詳細コピーを実行する必要があります。しかし、セルテンプレートにプロパティ値を格納するので、カスタム列にはCloneを上書きする必要はありません。クローニングはデザイナーにとって重要です。
  5. カスタムセルでは、InitializeEditingControlにカスタムセルのプロパティを使用して編集コントロールを設定する必要があります。また、編集コントロールへの参照を保持し、セルのプロパティのセッターで編集コントロールのプロパティを更新します。これは、ユーザーが列のプロパティが変更された場合に編集コントロールの変更が表示されることを期待するためです。カスタム編集コントロールで
  6. 、あなたは(ここではあなたのケースOnTextChangedで)値を変更することによって発生するイベントを処理し、その後、細胞はあなたが見ることができますthis.EditingControlDataGridView.NotifyCurrentCellDirty(true);

を使用して汚れてきたことをDataGridViewに通知する必要があります

関連する問題