wpf 将 DataTable 转换为 ObservableCollection(没有特定的类)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27489000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert DataTable to ObservableCollection (Without specific class)
提问by OetVV
I would like convert DataTable in ObservableCollection without having a specific classin my solution. (All others solution in stackoverflow.com is with specific class.)
我想在 ObservableCollection 中转换 DataTable而我的解决方案中没有特定的类。(stackoverflow.com 中的所有其他解决方案都具有特定的类。)
The reason is that the DataTable represent a Stored Procedure Result, and in the future I would like change the Stored Procedure result without any change in the application.
原因是DataTable代表了一个存储过程结果,以后我想改变存储过程结果而不对应用程序进行任何更改。
I can't bind directly my DataBale because I use Filter on my ModelView side, and I have an exception if I use DataTable and Filter together.
我无法直接绑定我的 DataBale,因为我在我的 ModelView 端使用了 Filter,如果我同时使用 DataTable 和 Filter,我会有一个例外。
Any idea ?
任何的想法 ?
Thanks
谢谢
The goal ?s to have the same code, but without BO.Line object.
目标是拥有相同的代码,但没有 BO.Line 对象。
static public ObservableCollection<BO.Line> GetValues()
{
DataTable _dataTable = DAL.ExecuteStoredProcedure(GetStoredProcedureNameInAppConfig());
ObservableCollection<BO.Line> _list = new ObservableCollection<BO.Line>();
foreach (DataRow _dataRow in _dataTable.Rows)
{
_list.Add(new BO.Line() { Instrument = _dataRow["Instrument"].ToString(),
Crystal = _dataRow["Crystal"].ToString() });
}
return _list;
}
In this sample:
在此示例中:
GetStoredProcedureNameInAppConfig() //return "GetLines"
but i would like change in my App.config (without recompile my application):
但我想更改我的 App.config(无需重新编译我的应用程序):
GetStoredProcedureNameInAppConfig() //return "GetPerson"
"GetLines"Stored Procedue return:
“GetLines”存储过程返回:
Instrument | Crystal
______________________
Instr1 | Crystal1
Instr1 | Crystal2
Instr2 | Crystal1
"GetPerson"Stored Procedue return:
“GetPerson”存储过程返回:
FirstName | LastName | Language
_______________________________
Alex | Doe | ENG
Britt | Nillis | ENG
Carlo | Petro | FR
Resume
I would like convert DataTable (with variable structure) in "generic" ObservableCollection. Witout specific class.
(ObservableCollection isn't good solution for me.)
Thank you
简历
我想在“通用”ObservableCollection 中转换 DataTable(具有可变结构)。没有特定的类。
(ObservableCollection 对我来说不是很好的解决方案。)
谢谢
回答by OetVV
This solution work with WPF Binding and Filter.
此解决方案适用于 WPF 绑定和过滤器。
Thank you to http://paulstovell.com/
public class GenericObject
{
private readonly ObservableCollection<GenericProperty> properties = new ObservableCollection<GenericProperty>();
public GenericObject(params GenericProperty[] properties)
{
foreach (var property in properties)
Properties.Add(property);
}
public ObservableCollection<GenericProperty> Properties
{
get { return properties; }
}
}
public class GenericProperty : INotifyPropertyChanged
{
public GenericProperty(string name, object value)
{
Name = name;
Value = value;
}
public string Name { get; private set; }
public object Value { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
private static ObservableCollection<GenericObject> Convert(DataTable toConvert)
{
ObservableCollection<GenericObject> _result = new ObservableCollection<GenericObject>();
foreach (DataRow _row in toConvert.Rows)
{
GenericObject _genericObject = new GenericObject();
foreach (DataColumn _column in toConvert.Columns)
{
_genericObject.Properties.Add(new GenericProperty(_column.ColumnName,_row[_column]));
}
_result.Add(_genericObject);
}
return _result;
}
Thank you all for your help.
谢谢大家的帮助。

