问题
I would like convert DataTable in ObservableCollection without having a specific class in my solution. (All others solution in stackoverflow.com is with specific class.)
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.
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.
Any idea ?
Thanks
The goal îs to have the same code, but without BO.Line object.
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):
GetStoredProcedureNameInAppConfig() //return "GetPerson"
"GetLines" Stored Procedue return:
Instrument | Crystal
______________________
Instr1 | Crystal1
Instr1 | Crystal2
Instr2 | Crystal1
"GetPerson" Stored Procedue return:
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
回答1:
This solution work with WPF Binding and Filter.
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.
来源:https://stackoverflow.com/questions/27489000/convert-datatable-to-observablecollection-without-specific-class