DataColumn.Exression and string formatting

  • Thread starter Thread starter A.M-SG
  • Start date Start date
A

A.M-SG

Hi,

I am truing to add a calculated column to my data table. The column must
show something like: "Date: 2005-01-23"

I underestand that we can use Convert to convert a DateTime value to string.
How can I convert it in specific date format?

Thank you,
Alan
 
It doesn't work.



The context is DataColumn.Expression syntax. What you said is C# syntax and
you cannot use it in DataColumn.Expression realm.



Regards,

Alan
 
Hi

Are you running a ASP.NET or winforms application?
Are you using a Datagrid?

Based on my research, here is some code about how to do that in a Grid view
in winform.
1. Set current thread's date pattern to "yyyy-MM-dd"

[STAThread]
static void Main()
{
CultureInfo ci =
(CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
Thread.CurrentThread.CurrentCulture = ci;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

2. For the string as "Date: xxxx-xx-xx"
private void Form1_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
//// Create the first column.
DataColumn dateColumn = new DataColumn();
dateColumn.DataType = System.Type.GetType("System.DateTime");
dateColumn.ColumnName = "date";
dateColumn.DefaultValue = "2002-4-18";

//// Create the second, calculated, column.
DataColumn testColumn = new DataColumn();
testColumn.DataType = System.Type.GetType("System.String");
testColumn.ColumnName = "Test";
testColumn.Expression = "'Date: ' + SubString(Convert(date,
'System.String'),1,10)";

// Add columns to DataTable.
table.Columns.Add(dateColumn);
table.Columns.Add(testColumn);

DataRow row = table.NewRow();
table.Rows.Add(row);
DataView view = new DataView(table);
this.dataGridView1.DataSource = view;
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A.M:

Do you need it in the DataColumn as such or do you just need it displayed in
a grid or other control? You can add a dataGridTableStyle, then a
dataGridColumnStyle if you're using Winforms to do this in a grid. You can
just specify the formatter if it's a textbox (although in 2005 there's a
MaskedTextBox which can do this for you). You can do the same with a ASP.NET
datagrid, just specifying the formatter
http://www.stevex.org/CS/blogs/dottext/articles/158.aspx
 
It is a winforms application. I like to set it up at the datatable level.
If it is not possible, then I have to do some custom code work.

In your example you changed the formatting behaviour globally. I cannot do
that.

Thank you for help.
Alan
 
I am trying to do that at DataSet level. I know how to do formatting at
presentation level.

Thank you,
Alan
 
Alan:

Just to make sure i understand the problem, is it that you're trying to
store a formatted String in a data column of type string?
 
No, I don't want to store anything.





I want to use DataColumn.Expression to have a calculated column

The calculated column is string concatenation between 3 other
columnscincluding a date column. I like to format that column to '
yyyy/MM/dd'



Does that make any sense?



Regards,
Alan
 
Hi Alan,

The expression is ususally used to do the mathematical or string operation,
it did not have many string format feature.
In the whole application view, the datatable should be in the data layer,
the string format should be taken care in the presentation layer.
e.g. the Winform DataGrid,GridView.......



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top