Format grid column to display "" instead of datetime DBNull (1/1/1

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Folks!

I have a grid that populates from a table on SQL Server. All datetime with
DBNull value show 1/1/1900 12:00AM ... Instead of just blank ("").

I found a document with the following code, but didn't work.

public void setGridColumnStyle()
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[0].GridColumnStyles[0];
newColStyle.NullText = "";
}

I tried to customize it to my needs as below:
public void setGridColumnStyle(ref System.Windows.Forms.Grid oGrid, string
sTable, string sColumn)
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[sTable].GridColumnStyles[sColumn];
newColStyle.NullText = "";
}

I always got an error on second line.

I will appreciate any help to do this.

Carlos Lozano
www.caxonline.net
 
Carlos said:
Hello Folks!

I have a grid that populates from a table on SQL Server. All datetime with
DBNull value show 1/1/1900 12:00AM ... Instead of just blank ("").

I found a document with the following code, but didn't work.

public void setGridColumnStyle()
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[0].GridColumnStyles[0];
newColStyle.NullText = "";
}

I tried to customize it to my needs as below:
public void setGridColumnStyle(ref System.Windows.Forms.Grid oGrid, string
sTable, string sColumn)
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[sTable].GridColumnStyles[sColumn];
newColStyle.NullText = "";
}

I always got an error on second line.

I will appreciate any help to do this.

Carlos Lozano
www.caxonline.net


What is the error? Is the error at runtime or design time...

Chris
 
Hi Chris,

The error is at runtime. It says "object not set to an instance". I know
the grid is not the problem as I can see it after it is populated.
I think it refers to the TableStyles object within the Grid. I Added the
following line to verify:

int nStyles = oGrid.TableStyles.Count;

nStyles value is zero. I was expecting a value of 1 as the grid was
populated from a table.

Just in case you are wondering, the column formatting setGridColumnStyle()
is called after the grid is populated.

Any Ideas?

Thank you,

Carlos Lozano

Chris said:
Carlos said:
Hello Folks!

I have a grid that populates from a table on SQL Server. All datetime with
DBNull value show 1/1/1900 12:00AM ... Instead of just blank ("").

I found a document with the following code, but didn't work.

public void setGridColumnStyle()
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[0].GridColumnStyles[0];
newColStyle.NullText = "";
}

I tried to customize it to my needs as below:
public void setGridColumnStyle(ref System.Windows.Forms.Grid oGrid, string
sTable, string sColumn)
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[sTable].GridColumnStyles[sColumn];
newColStyle.NullText = "";
}

I always got an error on second line.

I will appreciate any help to do this.

Carlos Lozano
www.caxonline.net


What is the error? Is the error at runtime or design time...

Chris
 
Carlos said:
Hi Chris,

The error is at runtime. It says "object not set to an instance". I know
the grid is not the problem as I can see it after it is populated.
I think it refers to the TableStyles object within the Grid. I Added the
following line to verify:

int nStyles = oGrid.TableStyles.Count;

nStyles value is zero. I was expecting a value of 1 as the grid was
populated from a table.

Just in case you are wondering, the column formatting setGridColumnStyle()
is called after the grid is populated.

Any Ideas?

Thank you,

Carlos Lozano

:

Carlos said:
Hello Folks!

I have a grid that populates from a table on SQL Server. All datetime with
DBNull value show 1/1/1900 12:00AM ... Instead of just blank ("").

I found a document with the following code, but didn't work.

public void setGridColumnStyle()
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[0].GridColumnStyles[0];
newColStyle.NullText = "";
}

I tried to customize it to my needs as below:
public void setGridColumnStyle(ref System.Windows.Forms.Grid oGrid, string
sTable, string sColumn)
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[sTable].GridColumnStyles[sColumn];
newColStyle.NullText = "";
}

I always got an error on second line.

I will appreciate any help to do this.

Carlos Lozano
www.caxonline.net


What is the error? Is the error at runtime or design time...

Chris

If newColStyle = oGrid.TableStyles[0].GridColumnStyles[0]; works and
newColStyle returns something and
oGrid.TableStyles[sTable].GridColumnStyles[sColumn]; returns nothing
then the names you are passing in for either sTable or sColumn is
returning nothing. Most likely sTable is the issue.

obj Object = oGrid.TableStyles[sTable];

Run that line of code and see if obj is set to null; if it does it
means that name of your tablestyle isn't equal sTable.

oGrid.TableStyles[0].TableName ??? there is some method that will give
you the name of your tablesytle, I don't know what it is called off the
top of my head.

hope it helps.
Chris
 
Hi Chris,

I just found where the problem is. The Grid does not have a tableStyles.
Grid.TableStyles.Count = 0 after creating the grid.
I added a TableStyle before populating it from a Table and i can handle the
grid's column properties (width, etc).

What I am still having trouble with isdatetime fields with dbNull values
still display as 1/1/1900 even when the ColumnStyle.NullText = "".

Thank you for your help.

Carlos Lozano
www.caxonline.net

Chris said:
Carlos said:
Hi Chris,

The error is at runtime. It says "object not set to an instance". I know
the grid is not the problem as I can see it after it is populated.
I think it refers to the TableStyles object within the Grid. I Added the
following line to verify:

int nStyles = oGrid.TableStyles.Count;

nStyles value is zero. I was expecting a value of 1 as the grid was
populated from a table.

Just in case you are wondering, the column formatting setGridColumnStyle()
is called after the grid is populated.

Any Ideas?

Thank you,

Carlos Lozano

:

Carlos Lozano wrote:

Hello Folks!

I have a grid that populates from a table on SQL Server. All datetime with
DBNull value show 1/1/1900 12:00AM ... Instead of just blank ("").

I found a document with the following code, but didn't work.

public void setGridColumnStyle()
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[0].GridColumnStyles[0];
newColStyle.NullText = "";
}

I tried to customize it to my needs as below:
public void setGridColumnStyle(ref System.Windows.Forms.Grid oGrid, string
sTable, string sColumn)
{
DataGridColumnStyle newColStyle;
newColStyle = oGrid.TableStyles[sTable].GridColumnStyles[sColumn];
newColStyle.NullText = "";
}

I always got an error on second line.

I will appreciate any help to do this.

Carlos Lozano
www.caxonline.net


What is the error? Is the error at runtime or design time...

Chris

If newColStyle = oGrid.TableStyles[0].GridColumnStyles[0]; works and
newColStyle returns something and
oGrid.TableStyles[sTable].GridColumnStyles[sColumn]; returns nothing
then the names you are passing in for either sTable or sColumn is
returning nothing. Most likely sTable is the issue.

obj Object = oGrid.TableStyles[sTable];

Run that line of code and see if obj is set to null; if it does it
means that name of your tablestyle isn't equal sTable.

oGrid.TableStyles[0].TableName ??? there is some method that will give
you the name of your tablesytle, I don't know what it is called off the
top of my head.

hope it helps.
Chris
 

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

Back
Top