2011-11-20 3 views
0

DGV에 다른 열을 추가 할 수 있었지만 이제는 값이있는 셀을 채우는 데 문제가 있습니다. DataGridView에서 사용자 지정 열 셀을 채우는 방법

// add new column 
int lineItemsColumnIndex = dataGridView1.Columns.Add("LineItems", "Line Items"); 

// set up nested DataGridView for orders and lineitems. 
foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    List<LineItem> lineItems = ((Order)(row.DataBoundItem)).lineItems; 
    string cellValue = ""; 
    for(int i=0; i<lineItems.Count; i++){ 
     FoodMenuItem menuItem = new FoodMenuItem(lineItems[i].menuItemID); 
     cellValue += lineItems[i].quantity.ToString() + "x " + menuItem.title + (i==lineItems.Count-1 ? "." : ", "); 
    } 
    row.Cells[lineItemsColumnIndex].Value = cellValue; 
} 

가 어떻게 사용자 정의 컬럼에 데이터를 배치해야합니까

: 여기

는 지금까지이 무엇입니까? 그것은 그런 식으로 작동합니다

답변

0

...

당신은 당신의 코드가 실행될 때 DGV 이미 행이 확신?

하지만 당신에 대해 생각할 수있는 다른 일이있다 :

당신이 당신의 데이터 객체에 대해 래퍼를 생성, 및/또는에서 사용할 수있는 데이터에 새로운 속성을 추가, 사용자 지정에서 TypeDescriptor를 추가 할 수 있습니다. 인터넷 데이터 바인딩, 당신은하지 않도록

뭔가 같은 수동으로 설치 열을 ...에 :

public class Wrapper<T> : System.ComponentModel.ICustomTypeDescriptor 
    { 
     public T wrappee { get; private set; } 
     private Dictionary<String, Func<T, String>> ext_get; 
     public Wrapper(T theObjectWeWantToWrap,Dictionary<String,Func<T,String>> theSpecsForTheAdditionalColumns) 
     { 
      wrappee = theObjectWeWantToWrap; 
      ext_get = theSpecsForTheAdditionalColumns; 
     } 

     System.ComponentModel.AttributeCollection System.ComponentModel.ICustomTypeDescriptor.GetAttributes() 
     { 
      return TypeDescriptor.GetAttributes(wrappee,true); 
     } 

     string System.ComponentModel.ICustomTypeDescriptor.GetClassName() 
     { 
      return TypeDescriptor.GetClassName(wrappee, true); 
     } 

     string System.ComponentModel.ICustomTypeDescriptor.GetComponentName() 
     { 
      return TypeDescriptor.GetComponentName(wrappee, true); 
     } 

     System.ComponentModel.TypeConverter System.ComponentModel.ICustomTypeDescriptor.GetConverter() 
     { 
      return TypeDescriptor.GetConverter(wrappee, true); 
     } 

     System.ComponentModel.EventDescriptor System.ComponentModel.ICustomTypeDescriptor.GetDefaultEvent() 
     { 
      return TypeDescriptor.GetDefaultEvent(wrappee, true); 
     } 

     System.ComponentModel.PropertyDescriptor System.ComponentModel.ICustomTypeDescriptor.GetDefaultProperty() 
     { 
      return TypeDescriptor.GetDefaultProperty(wrappee, true); 
     } 

     object System.ComponentModel.ICustomTypeDescriptor.GetEditor(Type editorBaseType) 
     { 
      return TypeDescriptor.GetEditor(wrappee, editorBaseType, true); 
     } 

     System.ComponentModel.EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents(Attribute[] attributes) 
     { 
      return TypeDescriptor.GetEvents(wrappee, attributes, true); 
     } 

     System.ComponentModel.EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents() 
     { 
      return TypeDescriptor.GetEvents(wrappee, true); 
     } 

     System.ComponentModel.PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties(Attribute[] attributes) 
     { 
      return filterProps(TypeDescriptor.GetProperties(wrappee, attributes, true)); 
     } 

     System.ComponentModel.PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties() 
     { 
      return filterProps(TypeDescriptor.GetProperties(wrappee, true)); 
     } 

     object System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd) 
     { 
      if (ext_get.Keys.Contains(pd.Name)) 
       return this; 
      return wrappee; 
     } 

     private PropertyDescriptorCollection filterProps(PropertyDescriptorCollection propertyDescriptorCollection) 
     { 
      List<PropertyDescriptor> pd_list = new List<PropertyDescriptor>(propertyDescriptorCollection.Cast<PropertyDescriptor>().Where(x=>!ext_get.Keys.Contains(x.DisplayName))); 
      foreach (var item in ext_get) 
      { 
       pd_list.Add(new MyPropertyDescriptor<T>(item)); 
      } 
      return new PropertyDescriptorCollection(pd_list.ToArray()); 
     } 
     private class MyPropertyDescriptor<T> : System.ComponentModel.PropertyDescriptor 
     { 
      public MyPropertyDescriptor(KeyValuePair<string,Func<T,string>>kvp) 
       :base(kvp.Key,new Attribute[]{}) 
      { 
       f = kvp.Value; 
      } 
      private Func<T, String> f; 

      public override bool CanResetValue(object component) 
      { 
       return false; 
      } 

      public override Type ComponentType 
      { 
       get { return typeof(Wrapper<T>); } 
      } 

      public override object GetValue(object component) 
      { 
       Wrapper<T> c = (Wrapper<T>)component; 
       return f(c.wrappee); 
      } 

      public override bool IsReadOnly 
      { 
       get { return true; } 
      } 

      public override Type PropertyType 
      { 
       get { return typeof(String); } 
      } 

      public override void ResetValue(object component) 
      { 
       throw new NotImplementedException(); 
      } 

      public override void SetValue(object component, object value) 
      { 
       throw new NotImplementedException(); 
      } 

      public override bool ShouldSerializeValue(object component) 
      { 
       return false; 
      } 
     } 
    } 

다음과 같이 사용될 수있다 :

당신이 DataBindingComplete 이벤트에 열을 추가 한 후 12,
0

는 다음과 같이 수행

foreach (DataGridViewRow row in dgv.Rows) 
{if (row.Cells[7].Value.ToString()=="1") 
row.Cells[0].Value = "number one"; } 

(설명하는 단지 바보 예)

을하지만 (IT가 DataBindingComplete에 있어야한다 기억 또는 버튼 클릭 등에서) 그렇지 않으면 빈 상태가됩니다.

관련 문제