![](/img/trans.png)
[英]Why is my datagrid not showing my data from my SQL query in WPF?
[英]Why is this WPF datagrid showing a time when importing data from SQL Server database with date datatype?
这是SQL Server Management Studio中的数据库数据:
这是在设计视图中:
这是datagrid的样子:
这是我用来将数据从SQL Server导入到datagrid中的代码。
SqlConnection connection = new SqlConnection(@"Data Source=.;Initial Catalog=TheatreDatabase2.0;Integrated Security=True");
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM performanceTable", connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
da.Fill(data);
dgdPerformance.ItemsSource = data.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
我想知道为什么数据网格中每个日期的末尾都有“ 12:00:00 AM”,以及我将如何删除它。
它使用默认的DateTime
表示形式,该表示形式在您的系统中来自线程当前的区域性。
如果要更改它,则应该手动派生列(使用DataGridTemplateColumn,并在CellTemplate
使用它):
<Textbox Text="{Binding Path=DateTimeValue, StringFormat=dd-MM-yyyy}" />
C#不只有Date
的数据类型,所以当你读这是类型的数据Date
从数据库中,它会被转换为DateTime
在C#。 您可以使用StringFormat
隐藏时间部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.