Edit only date in grid

A

Andrus

I need to display and edit only date part in DataGridView column whose data
source type is DateTime?.

The following code shows and allows to edit also time part.
How to modify this code so that only date is displayed and edited (without
time part) ?

Andrus.


using System;
using System.Windows.Forms;
using System.Collections.Generic;

public class Test {
static void Main() {
Application.Run(new Form1());
}}

class Form1 : Form {
public Form1() {
Controls.Add(new Grid()); }
}

class Grid : DataGridView {

public Grid() {
AutoGenerateColumns = false;
List<cust> l = new List<cust>();
l.Add(new cust());
DataGridViewTextBoxColumn tb = new DataGridViewTextBoxColumn();
tb.DataPropertyName = "c1";
Columns.Add(tb);
DataSource = l;
}
}

class cust {

public DateTime? c1 { get; set; }

public cust() {
c1 = DateTime.Now;
} }
 
M

Morten Wennevik [C# MVP]

Hi Andrus,

I'm sure there are lots of solutions to this. There might be a mask setting
somewhere. One solution would be to create your custom CellTemplate

class DateCellTemplate : DataGridViewTextBoxCell
{
public override Type FormattedValueType
{
get { return typeof(string); }
}

protected override object GetFormattedValue(object value, int
rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter
valueTypeConverter, TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
if (value == null)
return null;
DateTime date = (DateTime)value;
return date.ToShortDateString();
}

public override object ParseFormattedValue(object
formattedValue, DataGridViewCellStyle cellStyle, TypeConverter
formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
DateTime date = DateTime.MinValue;
if (DateTime.TryParse((string)formattedValue, out date))
return date;
else
return null;
}
}

If you set

tb.CellTemplate = new DateCellTemplate();

the textbox will display only the date part, but it does not prevent the
user to write a time value as well though you could override this by
returning only the date part of the DateTime from ParseFormattedValue
 
A

Andrus

Thank you.

Why you initialise date in

DateTime date = DateTime.MinValue;

date is passed as out parameter so initialization should be removed:

DateTime date;

Andrus.


Morten Wennevik said:
Hi Andrus,

I'm sure there are lots of solutions to this. There might be a mask
setting
somewhere. One solution would be to create your custom CellTemplate

class DateCellTemplate : DataGridViewTextBoxCell
{
public override Type FormattedValueType
{
get { return typeof(string); }
}

protected override object GetFormattedValue(object value, int
rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter
valueTypeConverter, TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
if (value == null)
return null;
DateTime date = (DateTime)value;
return date.ToShortDateString();
}

public override object ParseFormattedValue(object
formattedValue, DataGridViewCellStyle cellStyle, TypeConverter
formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
DateTime date = DateTime.MinValue;
if (DateTime.TryParse((string)formattedValue, out date))
return date;
else
return null;
}
}

If you set

tb.CellTemplate = new DateCellTemplate();

the textbox will display only the date part, but it does not prevent the
user to write a time value as well though you could override this by
returning only the date part of the DateTime from ParseFormattedValue

--
Happy Coding!
Morten Wennevik [C# MVP]


Andrus said:
I need to display and edit only date part in DataGridView column whose
data
source type is DateTime?.

The following code shows and allows to edit also time part.
How to modify this code so that only date is displayed and edited
(without
time part) ?

Andrus.


using System;
using System.Windows.Forms;
using System.Collections.Generic;

public class Test {
static void Main() {
Application.Run(new Form1());
}}

class Form1 : Form {
public Form1() {
Controls.Add(new Grid()); }
}

class Grid : DataGridView {

public Grid() {
AutoGenerateColumns = false;
List<cust> l = new List<cust>();
l.Add(new cust());
DataGridViewTextBoxColumn tb = new DataGridViewTextBoxColumn();
tb.DataPropertyName = "c1";
Columns.Add(tb);
DataSource = l;
}
}

class cust {

public DateTime? c1 { get; set; }

public cust() {
c1 = DateTime.Now;
} }
 

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