为什么此WPF数据网格显示从具有date数据类型的SQL Server数据库导入数据的时间?(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导入数据网格的代码。
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”,以及如何删除它。
This is what the database data looks like in SQL Server Management Studio:
Here it is in design view:
This is what the datagrid looks like:
This is the code that I am using to import the data from SQL Server into the 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(); }
I am wondering why there is the "12:00:00 AM" at the end of each date in the datagrid and how I would go about removing it.
原文:https://stackoverflow.com/questions/27759080
最满意答案
在这一行:
IEnumerable<int> listOfValues = property.GetValue(transformedData, null) as IEnumerable<int>;
您正在尝试在
List<SomeModel>
对象上检索ViewValue
属性的值,但List<SomeModel>
没有ViewValue
属性。 你可以做的一件事是:IEnumerable<int> listOfValues = transformedData.Select(i => (int)property.GetValue(i, null));
但更清洁的解决方案是完全避免反射,因为这里不需要:
interface IViewValue { int ViewValue { get; set; } } public class SomeViewModel : IViewValue { public int ViewValue { get; set; } } private int SumValues<T, TRes>(IEnumerable<T> data, Func<T, TRes> transformation) where TRes : IViewValue { var transformedData = data.Select(transformation); IEnumerable<int> listOfValues = transformedData.Select(i => i.ViewValue); var sum = listOfValues.Sum(); return sum; }
In this line:
IEnumerable<int> listOfValues = property.GetValue(transformedData, null) as IEnumerable<int>;
You're trying to retrieve the value of the
ViewValue
property on aList<SomeModel>
object, butList<SomeModel>
doesn't have aViewValue
property. One thing you could do is this:IEnumerable<int> listOfValues = transformedData.Select(i => (int)property.GetValue(i, null));
But a much cleaner solution would be to avoid reflection entirely as it's not needed here:
interface IViewValue { int ViewValue { get; set; } } public class SomeViewModel : IViewValue { public int ViewValue { get; set; } } private int SumValues<T, TRes>(IEnumerable<T> data, Func<T, TRes> transformation) where TRes : IViewValue { var transformedData = data.Select(transformation); IEnumerable<int> listOfValues = transformedData.Select(i => i.ViewValue); var sum = listOfValues.Sum(); return sum; }
相关问答
更多-
您可以使用 if(theType.IsGenericType && theType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) { Type underlyingType = theType.GetGenericArguments()[0]; //do something here } 编辑:添加了IsGenericType检查,感谢您的有用评论 You can use if(theType.IsGenericType && t ...
-
今天心理调试对我不起作用。 但我想我刚看到它。 您的HasManyToMany映射在映射中具有泛型类型声明。 删除这些类型。 Fluent应该能够通过你给它的lambda表达式推断出类型。 HasManyToMany
-
它不仅可以实现方法,而且必须实际实现接口。 在你的情况下,最好让你的类实现适当的接口。 public class SampleDataSource : IEnumerable
, IEnumerable 否则,你可以使用像linfu这样的东西将你的类打包到接口。 另一个潜在的对象是使用OfType 来处理这个: // returns enumerable of the items that are actually assignable to MyObject IEnumerable ... -
看起来您正在尝试在PropertyInfo对象上设置属性的值,该属性表示您要设置的属性,而不是在作为类实例的props上。 您还在循环遍历props的属性时第二次检索PropertyInfo ,因此我已将其删除。 我也假设一旦你使这个代码工作,你实际上将使用props做一些事情。 尝试以下: foreach (Control c in e.Page.Controls) { WizardProperties props = new WizardProperties(); SearchLookU ...
-
使用.NET 2.0,它有点尴尬,但不是太难: public static IEnumerable
Cast (IEnumerable source) where TDerived : TBase { foreach (TDerived item in source) { yield return item; } } 像这样调用它: IEnumerable pages = ... -
在这一行: IEnumerable
listOfValues = property.GetValue(transformedData, null) as IEnumerable ; 您正在尝试在List 对象上检索ViewValue属性的值,但List 没有ViewValue属性。 你可以做的一件事是: IEnumerable listOfValues = ... -
PropertyInfo.SetValue对象与目标类型不匹配(PropertyInfo.SetValue Object does not match target type)[2023-04-30]
错误似乎在这里: var objProp = dtoProps.SingleOrDefault(x => x.Name == dtoProp.Name); 我想你的意思是: var objProp = objProps.SingleOrDefault(x => x.Name == dtoProp.Name); The mistake seems to be here: var objProp = dtoProps.SingleOrDefault(x => x.Name == dtoProp.Name) ... -
如何使用Func
在Linq中指定.NET匿名对象返回类型?(How to specify .NET Anonymous object return type in Linq using Func [2022-08-14]?) 在这种情况下,你真正想要的只是int ...所以只需将你的选择改为: select DataAccessUtil.GetInt32(row, "ParentNodeID")); 并改变你的foreach: foreach (var nodeInfo in orderByLevelDesc(ds)) { if (fromNodeIds.Contains(nodeInfo)) return nodeInfo; } In this case all you really want is ... -
比较IEnumerable
的类型(Compare type of IEnumerable [2021-03-22]) 这对我来说是一个真正具有代码性的解决方案,它会降低效率,但您也可以在Type上迭代GetInterfaces()方法,如下所示: Listl = new List (); var t = l.GetType(); var ints = t.GetInterfaces(); 然后你可以对类型进行查找,如果不起作用,可以对它的接口进行查找。 然而 ,这感觉就像一个可怕的黑客,这通常表明需要做更多的设计工作。 是不是可以将List类型放在字典中? 有没有更好的方法来进 ... -
如果您可以编写一个可以使用单个DataRow构造具有“MyProperty”的对象实例的函数,那么您可以这样做: datatable.AsEnumerable().Select(MyConversionFunction); I ended up using Linq to convert the datatable into an array of anonymous objects. Dim objs = From row In dt Select New With { .Id = row.Fi ...