2009-03-11 7 views
2

일반 목록을 통해 바인딩하는 gridview가 있습니다. 나는 모든 칼럼을 직접 세웠다. 행이 숨겨진 필드를 통해 행 정보를 얻을 edited- 때DataGridView 바인딩

잡아라 이벤트 PRE 형식 오류 -이 꽤 쉽게해야합니다 확신

및 지속하지만 난하지 않은 : 난 그냥 노력하고 있어요 양식 작업을 많이했기 때문에 DataGridViews Events에 익숙하지 않습니다.

+0

(예 : 업데이트 참조) –

답변

6

두 가지 방법이 있습니다.

  • 은 멀리 UI에서이 논리를 소요하기 때문에

나는 보통 후자를 선호하는 특성에서 사용자 정의 TypeConverter를 사용 CellParsing 이벤트를 처리하고 값

  • 을 구문 분석; 내가 예를 할 수 있다면 나는 (이 코드의 대부분은 "작업 보여"코드) ...


    예를 볼 수 있습니다; 여기서 나는 "dd MMM yyyy"텍스트 (실제로 좋은 이유가 없음)로 날짜를 형식화/구문 분석하고 해당 변환기를 속성 중 하나와 연관 짓는 MyDateTimeConverter을 정의합니다. 그리드의 값을 편집 할 수 있으며 변경 사항이 다시 푸시됩니다 (행을 변경하여 "실제"값 업데이트 참조). 변경 알림 주위에 약간의 뉘앙스가 있기 때문에 즉시 표시되지 않습니다. 이 예제를 더 복잡하게 만들만한 가치가 없었습니다 ...

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Globalization; 
    using System.Windows.Forms; 
    
    class Person 
    { 
        public string Forename { get; set; } 
        public string Surname { get; set; } 
    
        [TypeConverter(typeof(MyDateTimeConverter))] 
        public DateTime EditableValue { get { return ActualValue; } set { ActualValue = value; } } 
        // this just proves what we have set... 
        public DateTime ActualValue { get; private set; } 
    } 
    class MyDateTimeConverter : TypeConverter 
    { 
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
        { 
         return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); 
        } 
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
        { 
         return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); 
        } 
        const string FORMAT = "dd MMM yyyy"; 
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) 
        { 
         if (value != null && value is string) 
         { 
          string s = (string)value; 
          return DateTime.ParseExact(Reverse(s), FORMAT, CultureInfo.InvariantCulture); 
         } 
         return base.ConvertFrom(context, culture, value); 
        } 
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
        { 
         if (destinationType == typeof(string)) 
         { 
          return Reverse(((DateTime)value).ToString(FORMAT, CultureInfo.InvariantCulture)); 
         } 
         return base.ConvertTo(context, culture, value, destinationType); 
        } 
        static string Reverse(string value) 
        { 
         char[] data = value.ToCharArray(); 
         Array.Reverse(data); 
         return new string(data); 
        } 
    } 
    class MyForm : Form 
    { 
        public MyForm() 
        { 
         DataGridView grid = new DataGridView(); 
         grid.Dock = DockStyle.Fill; 
         List<Person> people = new List<Person>(); 
         people.Add(new Person { Forename = "Fred", Surname = "Flintstone", EditableValue = DateTime.Today }); 
         people.Add(new Person { Forename = "Barney", Surname = "Rubble", EditableValue = DateTime.Today.AddDays(-25) }); 
         grid.DataSource = people; 
         Controls.Add(grid); 
        } 
        static void Main() 
        { 
         Application.EnableVisualStyles(); 
         Application.Run(new MyForm()); 
        } 
    } 
    
  • 관련 문제