views:

383

answers:

1

I have a gridview that I am binding to via a generic list. I have set all the columns myself. I am just trying to:

Catch the event PRE format error when a row is edited- get the row information via a hidden field - and persist

I am sure this must be pretty easy but I haven't done much with forms work and I am unfamiliar with its DataGridViews Events.

+3  A: 

There are two ways of looking at this;

  • handle the CellParsing event and parse the value
  • use a custom TypeConverter on the property

I usually prefer the latter, since it takes this logic away from the UI; I'll see if I can do an example...


Example (most of this code is the "show it working" code); here I define a MyDateTimeConverter, which formats/parses dates as their backwards "dd MMM yyyy" text (for no really good reason), and associate that converter with one of the properties. You can edit the values in the grid, and changes are pushed back in (change rows to see the "actual" value update). It doesn't show immediately because of some nuances around change-notification; it wasn't worth making the example more complex just for this...

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());
    }
}
Marc Gravell