Groups | Blog | Home
all groups > dotnet windows forms controls > june 2006 >

dotnet windows forms controls : DataGridView Column Format


Mike
6/23/2006 4:07:01 AM
Hi All,

I have been having difficulty formatting a column in a datagridview
The default display for a datetime shows the time with I don't want in this
case
So I thought easy just change the column format

The column is setup unbound such as this

this.TransDate.DataPropertyName = "TransDate";
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.Lime;
dataGridViewCellStyle1.Format = "d";
dataGridViewCellStyle1.NullValue = null;
this.TransDate.DefaultCellStyle = dataGridViewCellStyle1;
this.TransDate.HeaderText = "Date";
this.TransDate.Name = "TransDate";

The data comes from a binding list

gridRows = new BindingList<SalesInvoiceCompassSelectRow>(rows);
invoicesGrid.AutoGenerateColumns = false;
invoicesGrid.DataSource = gridRows;

Now my problem is whatever I put in the

dataGridViewCellStyle1.Format = "anything";

line it makes no difference to the format of the column

I tried "dd/MM/yyyy" "d" "D"
I know the dataGridViewCellStyle1 is being applied because it is lime green
which is part of the style.
It appears only the Format is not being applied.
I don't really want to use events if I can help it as it should work and is
much simpler/less code this way. I am bound to be doing this a lot in my app.

Any help much appreciated
--
Thanks

Using VS 2005 Pro

Mike
6/23/2006 7:14:02 AM
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


[quoted text, click to view]
Paul
6/23/2006 9:59:20 AM

[quoted text, click to view]

Mike,

I had a similar problem that was driving me nuts all afternoon yesterday.
Somehow the NullValue in the defaultCellStyle was getting set to null, but I
don't think that is something that you would ever want. NullValue is what
the Cell will display when the cell's underlying value is null, and it
really should be looking for a string (usually "" or "none"). I would try
and remove the line "dataGridViewCellStyle1.NullValue = null;" I can't say
for sure if this will fix your problem, but it definately screws up the
Parse/Format code when you use Nullable<> properties.

The question for MS is how did it get set in there. The designer window for
the DefaultCellStyle appears to set the value to a string normally, or
remove it altogether, and I can't repeat it getting set to null. But there
is no way that I would have set it manually in the .designer.cs.

Hope that helps,

-Paul

Paul
6/26/2006 11:24:40 AM

[quoted text, click to view]

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
6/28/2006 7:13:02 AM
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
7/12/2006 12:07:37 PM

[quoted text, click to view]

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

Mike
7/13/2006 1:35:01 AM
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]
AddThis Social Bookmark Button