2017-10-03 16 views
0

연장 된 DataGridView입니다. 오른쪽 클릭 옵션 인 "항목 선택"을 사용하여 다른 어셈블리에 대한 참조를 추가하지 않고 도구 상자에 새 컨트롤을 추가하고 싶습니다. 컨트롤은 폼의 같은 프로젝트에 있으며, 나는 그들을 구분하고 싶지 않습니다. 이것을 어떻게 할 수 있습니까?컨트롤이 동일한 프로젝트에있을 때 확장 된 컨트롤을 도구 상자에 추가하는 방법?

감사합니다.

편집 : 사용자 컨트롤이 아닌 경우 사용자 컨트롤에 관한 질문과 중복 될 수 없습니다.

편집 2 : 코드 자체 (이 과정에서 작품의 그것은하지 완성입니다.) :

class BindedDataGrid<T> : DataGridView 
{ 
    public BindedDataGrid() 
    { 
     InitializeComponent(); 

     IEnumerable<PropertyInfo> properties = typeof(T).GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute))); 

     foreach (PropertyInfo property in properties) 
     { 
      var column = new DataGridViewColumn() 
      { 
       HeaderText = ((property.GetCustomAttributes(true)[0]) as BindingValueAttribute).Value 
      }; 

      Columns.Add(column); 
     } 
    } 
} 
+0

흠. 프로젝트 구성 요소 중 하나로 정의되기로되어 있습니다. ProjectName_Components 탭에 표시됩니다. UserControl에서 상속 된 확장? –

+0

@o_O -이 질문은 사용자 컨트롤이 아닌 확장 컨트롤에 관한 것입니다. – Sipo

+0

내 잘못, [이 도움이 될 수도] (https://stackoverflow.com/questions/1116311/how-to-put-an-extended-winforms-control-on-toolbox) –

답변

0
public class BindedDataGrid : DataGridView 
    { 
     public BindedDataGrid() 
     { 
     } 

     public BindedDataGrid(Type bindType) 
     { 
      IEnumerable<PropertyInfo> properties = bindType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(BindingValueAttribute))); 

      foreach (PropertyInfo property in properties) 
      { 
       var prop = property.GetCustomAttributes(true)[0]; 
       if (prop is BindingValueAttribute) 
       { 
        var column = new DataGridViewColumn() 
        { 
         HeaderText = property.Name 
        }; 
        column.CellTemplate = new DataGridViewTextBoxCell(); 
        Columns.Add(column); 
       } 
      } 
     } 
    } 

    public class BindingValueAttribute : Attribute 
    { 
     public string Value { get; set; } 
    } 

    public class BindOne 
    { 
     [BindingValueAttribute()] 
     public string Name { get; set; } 

     [BindingValueAttribute()] 
     public int Age { get; set; } 
    } 
  1. 모두의 첫번째 만들
  2. 비를 만들

  3. 일반적인 BindedDataGrid 없음 매개 변수화 된 생성자

    private void InitializeComponent() 
    { 
        this.bindedDataGrid1 = new PlayingWithThread.BindedDataGrid(typeof(BindOne)); 
        ((System.ComponentModel.ISupportInitialize)(this.bindedDataGrid1)).BeginInit();