2009-06-12 11 views
7

に異なる制御タイプを持っていることのDataGridViewで次の操作を実行することが可能ですDataGridViewコントロールをフォーム:Windowsは、同じ列

私はDataGridViewTextBoxColumnとDataGridViewComboBoxColumn間、各行のコントロールの種類を変更したい、同じ列に?

(これは、ドロップダウンリストを表示したいときや、ユーザーがフリーハンドの値を入力したい場合があるためです)。ありがとうございます。

P.S.私はC#を使用しています

+0

非常に良い質問です。 DataGridViewクラスを使用しているときに、この問題に直面するまでは時間の問題です。 – TheBlastOne

答えて

0

DataGridViewCellから継承した独自のクラスを作成し、適切な仮想メンバーをオーバーライドすることができます(InitializeEditingControls、EditType、多分その他)。あなたは、セルテンプレート

+0

ありがとうございます。正しいトラックのように見えます。残念ながら、私はこれを新しくしていて、私が何をしても細胞が出現することはできません。私は、提案されたようにEditTypeをオーバーライドしました。私は 'DataGridViewTextBoxCell'を継承すると、 'DataGridViewCell'を継承してtypeof(DataGridViewTextBoxEditingControl)を返すEditTypeをオーバーライドしてセルを見ることができます。空のセルが表示されます。明らかに私が見逃したことがあると確信しています。 –

+0

ビューモードコントロールが表示されていないか、ゼロの場合は幅がありますか?コントロールにタブで移動し、F2キーを押して値を入力します。私が終了すると、私はそれを見ることができません、私は編集モードに戻ってもまだそこにあります。 –

+0

私はPositionEditingPanelとPositionEditingControlもオーバーライドする必要があると思います。しかし、私はそれを自分自身をやったことがないので、私は唯一のあなたは、それはおそらくあなたが –

0

両方のコントロールを含むテンプレート列を作成し、不要なものを非表示にし、もう一方をコードのデータソースにバインドすることができます。

+0

テンプレート列は、ASP.NET GridView用であり、WinForms DataGridView用ではありません。 –

+0

Ahh、my bad。私がWinFormsで作業して以来、しばらく経っています。 – atfergs

7

として、このクラスのインスタンスでDataGridViewColumnを作成することができます私は最近、同様のユースケースがあったが、以下のようなコードを書いてしまっている:

はカスタムセルと列クラスを書きます、および、セルのEditTypeとInitializeEditingControlメソッドをオーバーライドし、必要に応じてさまざまなコントロールを返すために(ここで私は使用する制御内容を示す.useComboフィールドとカスタムクラスのリストに、データバインディングしています):

// Define a column that will create an appropriate type of edit control as needed. 
public class OptionalDropdownColumn : DataGridViewColumn 
{ 
    public OptionalDropdownColumn() 
     : base(new PropertyCell()) 
    { 
    } 

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

// And the corresponding Cell type 
public class OptionalDropdownCell : DataGridViewTextBoxCell 
{ 

    public OptionalDropdownCell() 
     : base() 
    {   
    } 

    public override void InitializeEditingControl(int rowIndex, object 
     initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
    { 
     // Set the value of the editing control to the current cell value. 
     base.InitializeEditingControl(rowIndex, initialFormattedValue, 
      dataGridViewCellStyle); 

     DataItem dataItem = (DataItem) this.OwningRow.DataBoundItem; 
     if (dataItem.useCombo) 
     { 
      DataGridViewComboBoxEditingControl ctl = (DataGridViewComboBoxEditingControl)DataGridView.EditingControl; 
      ctl.DataSource = dataItem.allowedItems; 
      ctl.DropDownStyle = ComboBoxStyle.DropDownList; 
     } 
     else 
     { 
      DataGridViewTextBoxEditingControl ctl = (DataGridViewTextBoxEditingControl)DataGridView.EditingControl; 
      ctl.Text = this.Value.ToString(); 
     } 
    } 

    public override Type EditType 
    { 
     get 
     { 
      DataItem dataItem = (DataItem)this.OwningRow.DataBoundItem; 
      if (dataItem.useCombo) 
      { 
       return typeof(DataGridViewComboBoxEditingControl); 
      } 
      else 
      { 
       return typeof(DataGridViewTextBoxEditingControl); 
      } 
     } 
    } 
} 

次に、このタイプのDataGridViewに列を追加するだけです。正しい編集コントロールは、 sed。

+0

+1、これはかなり役に立ち、きれいに見えます。このことは、EditTypeゲッターで必要なエディットコントロールクラスを「ちょうどいいタイミングで」知らせるだけでいいので、グリッドを頑張らせ、DataGridView.EditingControlを参照してそのように作成したエディットコントロールを使用することができます。 – TheBlastOne

関連する問題