By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.


Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Dom
On Apr 23, 2:04*pm, "TheSteph" <TheSt...@NoSpam.comwrote:
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.
Jun 27 '08 #2

P: n/a
On Apr 23, 2:04*pm, "TheSteph" <TheSt...@NoSpam.comwrote:
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 :)
Jun 27 '08 #3

P: n/a
On Apr 23, 2:04*pm, "TheSteph" <TheSt...@NoSpam.comwrote:
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
Jun 27 '08 #4

P: n/a

Check out the Format and Parse events on the cells. In the following
example, the data is stored as 75000 but the user wants the data displayed
as "75k" instead. This converts it from 75000 to 75k for display, and
converts it back when going the other way. You should be able to do this to
parse the dates.

CellFormatting happens between the data source and the screen.
CellParsing happens between the screen and the data source.

myGrid.CelFormatting += OnCellFormatting;
myGrid.CellParsing += OnCellParsing;

private sub OnCellFormatting(sender object,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == myGrid.Columns("columnIcareAbout"))
{
//setting this to true signals the grid that this
// column is being dynamically updated. If you don't
// do this, you will get an infinite loop if you have
// also set the column to have its size automatically
// determined.
e.FormattingApplied = true;

//get the row being populated; format this cell
if (e.Value == "75000")
e.Value = "75K";
}
}

private sub OnCellParsing(sender object, DataGridViewCellParsingEventArgs e)
{
if (e.ColumnIndex == myGrid.Columns("columnIcareAbout"))
{
//get the row being populated; format this cell
if (e.Value == "75K")
e.Value = "75000";

}
}
RobinS.
GoldMail.com

"TheSteph" <Th******@NoSpam.comwrote in message
news:e$*************@TK2MSFTNGP05.phx.gbl...
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.

Jun 27 '08 #5

P: n/a
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);

}
}
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.