Hi Paul
Did the simplest project I could.
The DateTime column still displays as
28/06/2006 15:10:20
I can't get rid of the time.
Hope you can help
Thanks
Mike
Here is the code
1.Drop a datagridview on a form.
2.Here is the form code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestDateFormat
{
public partial class Form1 : Form
{
private System.Collections.ObjectModel.Collection<TestRow> dates;
public Form1()
{
InitializeComponent();
dates = new System.Collections.ObjectModel.Collection<TestRow>();
TestRow row = new TestRow();
row.TransDate = DateTime.Now;
dates.Add(row);
dataGridView1.Columns["DateColumn"].DefaultCellStyle.Format =
"dd/MM/yyyy";
dataGridView1.Columns["DateColumn"].DataPropertyName =
"TransDate";
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dates;
}
}
}
3.Here is the TestRowClass.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlTypes;
namespace TestDateFormat
{
class TestRow
{
private SqlDateTime transDate;
public SqlDateTime TransDate
{
get { return transDate; }
set { transDate = value; }
}
}
}
--
Thanks
Using VS 2005 Pro
Mike
[quoted text, click to view] "Paul" wrote:
>
> "Mike" <Mike@discussions.microsoft.com> wrote in message
> news:67E68B97-3AB7-4A9A-ADBE-CEA061CBB407@microsoft.com...
> > Hi Paul
> >
> > I tried setting NullValue property to "" and also commenting it out in the
> > designer partial class.
> > I have no null values in the displayed column so I would not expect it to
> > run any code against this property.
> >
> > Unfortunately the problem remains the same. The forecolor is applied but
> > the Format property makes no difference to the column display.
> >
> > I'm sorry I don't have any more info. I wopuld have expected this to be a
> > common issue as it is common to want to suppress the Time portion of a
> > database field.
> >
> > --
> > Thanks
> >
> > Using VS 2005 Pro
> >
> > Mike
> >
> >
>
> Mike,
>
> The only suggestion I can make would be to create a new, simple project that
> only handles a one row, one (DateTime) column list, and a DataGridView that
> formats it. Make sure that works, and then start building up the code
> towards your current implementation one bit at a time until it stops
> working. If that isolates it, great, post the solution back here. If not,
> post the simple code that does not work and we can try and figure out why.
>
> -Paul
>
>
[quoted text, click to view] "Mike" <Mike@discussions.microsoft.com> wrote in message
news:E92793D6-1CBD-4EB3-98A9-A83A238C3048@microsoft.com...
> Hi Paul
>
> Did the simplest project I could.
> The DateTime column still displays as
>
> 28/06/2006 15:10:20
>
> I can't get rid of the time.
>
> Hope you can help
>
> Thanks
>
> Mike
>
> Here is the code
>
> 1.Drop a datagridview on a form.
> 2.Here is the form code
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
>
> namespace TestDateFormat
> {
> public partial class Form1 : Form
> {
> private System.Collections.ObjectModel.Collection<TestRow> dates;
>
> public Form1()
> {
> InitializeComponent();
> dates = new
> System.Collections.ObjectModel.Collection<TestRow>();
> TestRow row = new TestRow();
> row.TransDate = DateTime.Now;
> dates.Add(row);
> dataGridView1.Columns["DateColumn"].DefaultCellStyle.Format =
> "dd/MM/yyyy";
> dataGridView1.Columns["DateColumn"].DataPropertyName =
> "TransDate";
> dataGridView1.AutoGenerateColumns = false;
> dataGridView1.DataSource = dates;
>
> }
> }
> }
>
> 3.Here is the TestRowClass.
>
> using System;
> using System.Collections.Generic;
> using System.Text;
> using System.Data.SqlTypes;
>
> namespace TestDateFormat
> {
> class TestRow
> {
> private SqlDateTime transDate;
> public SqlDateTime TransDate
> {
> get { return transDate; }
> set { transDate = value; }
> }
> }
> }
>
>
> --
> Thanks
>
> Using VS 2005 Pro
>
> Mike
>
>
> "Paul" wrote:
>
>>
>> "Mike" <Mike@discussions.microsoft.com> wrote in message
>> news:67E68B97-3AB7-4A9A-ADBE-CEA061CBB407@microsoft.com...
>> > Hi Paul
>> >
>> > I tried setting NullValue property to "" and also commenting it out in
>> > the
>> > designer partial class.
>> > I have no null values in the displayed column so I would not expect it
>> > to
>> > run any code against this property.
>> >
>> > Unfortunately the problem remains the same. The forecolor is applied
>> > but
>> > the Format property makes no difference to the column display.
>> >
>> > I'm sorry I don't have any more info. I wopuld have expected this to
>> > be a
>> > common issue as it is common to want to suppress the Time portion of a
>> > database field.
>> >
>> > --
>> > Thanks
>> >
>> > Using VS 2005 Pro
>> >
>> > Mike
>> >
>> >
>>
>> Mike,
>>
>> The only suggestion I can make would be to create a new, simple project
>> that
>> only handles a one row, one (DateTime) column list, and a DataGridView
>> that
>> formats it. Make sure that works, and then start building up the code
>> towards your current implementation one bit at a time until it stops
>> working. If that isolates it, great, post the solution back here. If
>> not,
>> post the simple code that does not work and we can try and figure out
>> why.
>>
>> -Paul
>>
>>
>>
Mike,
Sorry I did not get back to you sooner... on vacation.
The problem is that SqlDateTime does not have a ToString() that takes a
format string, as DateTime does. The cell formatting routine of the
DataGridView tries to call a ToString(string) on the object in question if
there is one, and if not, it will just call ToString() without the format
string.
You have 2 options:
- use a DateTime (or a DateTime? if you need nullability) instead
- write a CellFormatting Event Handler which formats for you
If you want to add a handler, you would do the following:
- add the handler to the grid view in the constructor:
dataGridView1.CellFormatting += new
DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
- write the routine
void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.Value.GetType() == typeof(SqlDateTime))
{
SqlDateTime a_sqlDateTime = (SqlDateTime)e.Value;
if (a_sqlDateTime.IsNull == false)
e.Value =
a_sqlDateTime.Value.ToString(dataGridView1.Columns["DateColumn"].DefaultCellStyle.Format);
}
}
Hope this helps,
-Paul
Hi Paul
Thanks for that. I had worked out it was the datatype (with some help) but
not the exact explanation.
With regard to ToString(string) I think there might be an argument for an
exception from the DataGridView in the case where the datatype does not
support a format string rather than reverting to ToString()
ie "The DataType for Column A does not support a format sting"
The reason I was using SqlDateTime was becuase the DAL I was using was
returning these types. I had a look at the dataset object (becuase that
works fine) and see it converts all the sql types to native nullable types.
Wouldn't it be great to have a common type system for sqlserver and .net.
Thanks
Mike
--
Thanks
Using VS 2005 Pro
Mike
[quoted text, click to view] "Paul" wrote:
>
> "Mike" <Mike@discussions.microsoft.com> wrote in message
> news:E92793D6-1CBD-4EB3-98A9-A83A238C3048@microsoft.com...
> > Hi Paul
> >
> > Did the simplest project I could.
> > The DateTime column still displays as
> >
> > 28/06/2006 15:10:20
> >
> > I can't get rid of the time.
> >
> > Hope you can help
> >
> > Thanks
> >
> > Mike
> >
> > Here is the code
> >
> > 1.Drop a datagridview on a form.
> > 2.Here is the form code
> >
> > using System;
> > using System.Collections.Generic;
> > using System.ComponentModel;
> > using System.Data;
> > using System.Drawing;
> > using System.Text;
> > using System.Windows.Forms;
> >
> > namespace TestDateFormat
> > {
> > public partial class Form1 : Form
> > {
> > private System.Collections.ObjectModel.Collection<TestRow> dates;
> >
> > public Form1()
> > {
> > InitializeComponent();
> > dates = new
> > System.Collections.ObjectModel.Collection<TestRow>();
> > TestRow row = new TestRow();
> > row.TransDate = DateTime.Now;
> > dates.Add(row);
> > dataGridView1.Columns["DateColumn"].DefaultCellStyle.Format =
> > "dd/MM/yyyy";
> > dataGridView1.Columns["DateColumn"].DataPropertyName =
> > "TransDate";
> > dataGridView1.AutoGenerateColumns = false;
> > dataGridView1.DataSource = dates;
> >
> > }
> > }
> > }
> >
> > 3.Here is the TestRowClass.
> >
> > using System;
> > using System.Collections.Generic;
> > using System.Text;
> > using System.Data.SqlTypes;
> >
> > namespace TestDateFormat
> > {
> > class TestRow
> > {
> > private SqlDateTime transDate;
> > public SqlDateTime TransDate
> > {
> > get { return transDate; }
> > set { transDate = value; }
> > }
> > }
> > }
> >
> >
> > --
> > Thanks
> >
> > Using VS 2005 Pro
> >
> > Mike
> >
> >
> > "Paul" wrote:
> >
> >>
> >> "Mike" <Mike@discussions.microsoft.com> wrote in message
> >> news:67E68B97-3AB7-4A9A-ADBE-CEA061CBB407@microsoft.com...
> >> > Hi Paul
> >> >
> >> > I tried setting NullValue property to "" and also commenting it out in
> >> > the
> >> > designer partial class.
> >> > I have no null values in the displayed column so I would not expect it
> >> > to
> >> > run any code against this property.
> >> >
> >> > Unfortunately the problem remains the same. The forecolor is applied
> >> > but
> >> > the Format property makes no difference to the column display.
> >> >
> >> > I'm sorry I don't have any more info. I wopuld have expected this to
> >> > be a
> >> > common issue as it is common to want to suppress the Time portion of a
> >> > database field.
> >> >
> >> > --
> >> > Thanks
> >> >
> >> > Using VS 2005 Pro
> >> >
> >> > Mike
> >> >
> >> >
> >>
> >> Mike,
> >>
> >> The only suggestion I can make would be to create a new, simple project
> >> that
> >> only handles a one row, one (DateTime) column list, and a DataGridView
> >> that
> >> formats it. Make sure that works, and then start building up the code
> >> towards your current implementation one bit at a time until it stops
> >> working. If that isolates it, great, post the solution back here. If
> >> not,
> >> post the simple code that does not work and we can try and figure out
> >> why.
> >>
> >> -Paul
> >>
> >>
> >>
>
> Mike,
>
> Sorry I did not get back to you sooner... on vacation.
>
> The problem is that SqlDateTime does not have a ToString() that takes a
> format string, as DateTime does. The cell formatting routine of the
> DataGridView tries to call a ToString(string) on the object in question if
> there is one, and if not, it will just call ToString() without the format
> string.
>
> You have 2 options:
> - use a DateTime (or a DateTime? if you need nullability) instead
> - write a CellFormatting Event Handler which formats for you
>
> If you want to add a handler, you would do the following:
>
> - add the handler to the grid view in the constructor:
>
> dataGridView1.CellFormatting += new
> DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
>
> - write the routine
>
> void dataGridView1_CellFormatting(object sender,
> DataGridViewCellFormattingEventArgs e)
> {
> if (e.Value.GetType() == typeof(SqlDateTime))
> {
> SqlDateTime a_sqlDateTime = (SqlDateTime)e.Value;
> if (a_sqlDateTime.IsNull == false)
> e.Value =
> a_sqlDateTime.Value.ToString(dataGridView1.Columns["DateColumn"].DefaultCellStyle.Format);
> }
> }
>
> Hope this helps,
>
> -Paul
>
>
Don't see what you're looking for? Try a search.