473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 6325
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jjouett | last post by:
I'm trying to preserve the newline characters in my transformed XML (read from a file) to provide a meaningful line number when validation errors occur. However, my current method which performs an...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
9
by: Matt | last post by:
In many places in my application, I have a DataGridView which users can either enter data into or use as a method of navigation in a master/detail form view. I need a method of stopping the user...
2
by: Pieter | last post by:
Hi, I'm using a DataGridView with a DataGridViewCheckBoxColumn. When the users checks or unchecks the checkbox, some actions have to happen in the underlying DataSource and other objects. But...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
5
by: DanThMan | last post by:
The situation: * I have a ButtonColumn in a DataGridView. * When the user preses one of the buttons, a dialog appears. * Based on what the user selects in the dialog, data is entered...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
1
by: Roach | last post by:
VB.NET 2005 2.0 Framework application is using a DataGridView for SQL data access and user data entry. As part of exception/error handling, the app wants to advise the user when he/she enters a...
9
by: Miro | last post by:
My current headache is proper is with the datagridview I am starting to realize that a DataGridView within vs2008 is not as 'robust' as a 'textboxfield' by default for example. Example: A...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.