[英]DataGrid copy to clipboard DateTime format issue
I have a data grid in WPF that I am filling with an SQL query result through a data table. 我在WPF中有一个数据网格,正在通过数据表填充SQL查询结果。 However, when I copy the grid to clipboard, it is always copied with the full date/time format.
但是,当我将网格复制到剪贴板时,它总是以完整的日期/时间格式复制。 Here is what I am doing:
这是我在做什么:
I have an SQL statement to read some data including date field from the DB as: 我有一条SQL语句从数据库读取一些数据,包括日期字段,如:
searchProDateSQLCMD.CommandText = "select pName, convert(date, pDate), FROM [Professionals] WHERE pName = @parName"
I fill a data table with the result as: 我用以下结果填充数据表:
dTableAdpater = new SqlDataAdapter(searchProDateSQLCMD);
dTable = new DataTable("Professionals");
dTableAdpater.Fill(dTable);
I then set the column for the grid to display the info: 然后,我为网格设置列以显示信息:
DataGridTextColumn jDateCl = new DataGridTextColumn();
pDateCl.Binding = new Binding("pDate");
pDateCl.Binding.StringFormat = "{0:d}";
I then bind the grid with the column I created 然后,将网格与我创建的列绑定
SearchResultGrid.Columns.Add(pDateCl);
When I try to copy that column: 当我尝试复制该列时:
SearchResultGrid.SelectAllCells();
SearchResultGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
I get the time copied; 我得到了时间。 but with the time format is
(dd/MM/yyyy 12:00:00 AM)
, even though I use convert()
in SQL, and {0:d}
in the column. 但时间格式为
(dd/MM/yyyy 12:00:00 AM)
,即使我在SQL中使用convert()
在列中使用{0:d}
。
What am I doing wrong? 我究竟做错了什么? What am I missing to just get the date on copy without the time?
我只是想在没有时间的情况下得到复印件而错过了什么?
Unfortunately, this is a current DataGrid
's implementation limitation. 不幸的是,这是当前
DataGrid
的实现限制。
As you can see in the reference sources , the DataGrid
renders the cell's contents into the clipboard using a simple ToString()
call, without providing any format. 从参考资料中可以看到,
DataGrid
使用简单的ToString()
调用将单元格的内容呈现到剪贴板中,而没有提供任何格式。 This call will use the CultureInfo.CurrentCulture
and the default format for the DateTime
, hence the result you get. 此调用将使用
CultureInfo.CurrentCulture
和DateTime
的默认格式,因此将得到结果。
So you have at least two options: 因此,您至少有两个选择:
Set the main thread's CultureInfo
to the the desired date/time format. 将主线程的
CultureInfo
设置为所需的日期/时间格式。 Note that this will affect all the ToString()
calls on any DateTime
objects, so maybe this is not suitable for you. 请注意,这将影响任何
DateTime
对象上的所有ToString()
调用,因此这可能不适合您。
Instead of using a DateTime
column, use a string
column and do the conversion on the server side. 代替使用
DateTime
列,而使用string
列并在服务器端进行转换。 Not a good approach however, because you loose all the benefits of the DateTime
type. 但是,这不是一个好方法,因为您失去了
DateTime
类型的所有好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.