如何检查datagrid中的列表类型 [英] How to check type of list in datagrid
问题描述
我有一个带有数据网格的表单,使用自动生成列,我将
dataGrid.ItemsSource设置为不同的列表以显示不同的结果。
表格上还有一个按钮,当我点击它时,我想查看当前显示的列表。
检查dataGrid.ItemsSource.GetType()告诉我类型,但如果我尝试这个,编译器告诉我它永远不会是那种类型:
I have a form with a datagrid using auto generate collumns and I set
dataGrid.ItemsSource to different lists to show different result.
I also have a button on the form and when I click on it I want to check what list is displayed currently.
Checking dataGrid.ItemsSource.GetType() tells me the type but if I try this the complier tells me it will never be that type:
if ( dataGrid.ItemsSource.GetType() is LogLines)
< br $>
在调试窗口中打印时GetType的内容是
System.Collections.Generic.List`1 [AnalyzeLog.LogLines]
所以如何更改我的if语句以便我可以做根据数据网格中的数据不同的东西?
我可以定义一个可用的公共变量在应用程序级别上并且以这种方式跟踪它但我不喜欢这种方法。
The content of GetType when printed in the debug window is
System.Collections.Generic.List`1[AnalyzeLog.LogLines]
so how should I change my if statement so I can do different things based on the data in the datagrid ?
I could just define a public variable that is available on application level and keep track of it that way but I don't like that approach.
推荐答案
我假设LogLines是列表中的对象类型。
因此,您获得的类型是包含LogLines对象的通用集合。
您可以更改if语句以检查:
I assume LogLines is the type of object that is in the list.
So, the type you're getting is a generic collection that contains LogLines objects.
You can changed your if statement to check for:
if ( dataGrid.ItemsSource.GetType() is IEnumerable<loglines>)</loglines>
这应该可以解决问题。
That should do the trick.
这篇关于如何检查datagrid中的列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!