DataGridView : How to Transform data between user input and validation ?

T

TheSteph

Hi !

I have a DataGridView with a Date (DateTime) Column.

When a user edit the cell and change the date I woulk like to allow him to
write "081501" and programmatically transform the entered date to
"2008/15/01"( using a custom method called TransformDate(string ARawDate))

But I can't find any DataGridView event to call the TransformDate method and
replace the user input by the computed value. I always get a "DataError".

Does anybody if this kind of transformation is possible in a DataGridView
and Where is the best place to put the call to the transformation method ?

Thanks for your help !

Steph.
 
D

Dom

Hi !

I have a DataGridView with a Date (DateTime) Column.

When a user edit the cell and change the date I woulk like to allow him to
write "081501" and programmatically transform the entered date to
"2008/15/01"( using a custom method  called TransformDate(string ARawDate))

But I can't find any DataGridView event to call the TransformDate method and
replace the user input by the computed value. I always get a "DataError".

Does anybody if this kind of transformation  is possible in a DataGridView
and Where is the best place to put the call to the transformation method ?

Thanks for your help !

Steph.

Try the CellValidating event. Also, the EventArgs in this event has a
"Cancel" member, which prevents the user from leaving the cell, if it
does not validate.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi !

I have a DataGridView with a Date (DateTime) Column.

When a user edit the cell and change the date I woulk like to allow him to
write "081501" and programmatically transform the entered date to
"2008/15/01"( using a custom method  called TransformDate(string ARawDate))

But I can't find any DataGridView event to call the TransformDate method and
replace the user input by the computed value. I always get a "DataError".

Does anybody if this kind of transformation  is possible in a DataGridView
and Where is the best place to put the call to the transformation method ?

Thanks for your help !

Steph.

Hi,

There are a couple of events you could try:
CellEndEdit
CellLeave
CellValidating

Of course the later seems the correct one :)
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi !

I have a DataGridView with a Date (DateTime) Column.

When a user edit the cell and change the date I woulk like to allow him to
write "081501" and programmatically transform the entered date to
"2008/15/01"( using a custom method  called TransformDate(string ARawDate))

But I can't find any DataGridView event to call the TransformDate method and
replace the user input by the computed value. I always get a "DataError".

Does anybody if this kind of transformation  is possible in a DataGridView
and Where is the best place to put the call to the transformation method ?

Thanks for your help !

Steph.

Hi,

Note that for consistence you should also have the other way around
conversion, when the user start editing a cell the cell should
transform fro 2008/15/01 to 081501
 
M

Marc Gravell

One other option; if you use the property in multiple places (and you
don't want have to add this code everywhere), then you can write your
own TypeConverter (below); but in any event, you are going to have a
tricky time internationalizing it... here I've gone for a simple
"replace abcdef with ab/cd/ef" approach.

Marc

using System;
using System.ComponentModel;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form
{
Controls =
{
new DataGridView {
Dock = DockStyle.Fill,
DataSource = new[]
{
new Foo {Bar = DateTime.Today},
new Foo {Bar = new DateTime(2001, 04, 05)}
}
}
}
});
}
}
class Foo
{
[TypeConverter(typeof(CustomDateTimeConverter))]
public DateTime Bar { get; set; }
}
class CustomDateTimeConverter : DateTimeConverter
{
static readonly Regex pattern = new Regex("([0-9]{2})([0-9]{2})
([0-9]{2})", RegexOptions.Compiled);
static object ExpandDate(object value)
{
string text = value as string;
if(text != null && pattern.IsMatch(text))
{
value = pattern.Replace(text, "$1/$2/$3");
}
return value;
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
value = ExpandDate(value);
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
if(destinationType == typeof(string) && value is DateTime)
{
DateTime when = (DateTime)value;
return when.ToShortDateString();
}
return base.ConvertTo(context, culture, value,
destinationType);

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top