将DataTable转换为ObservableCollection(没有特定的类) [英] Convert DataTable to ObservableCollection (Without specific class)

查看:389
本文介绍了将DataTable转换为ObservableCollection(没有特定的类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ObservableCollection 中转换DataTable,而在我的解决方案中没有特定的类. (stackoverflow.com中的所有其他解决方案均具有特定的类.)

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.)

原因是DataTable表示存储过程结果,将来我希望在不对应用程序进行任何更改的情况下更改存储过程结果.

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.

我无法直接绑定我的DataBale,因为我在ModelView端使用了Filter,如果同时使用DataTable和Filter,则会出现异常.

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.

有什么主意吗?

谢谢

目标是具有相同的代码,但没有BO.Line对象.

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;
    }

在此示例中:

GetStoredProcedureNameInAppConfig() //return "GetLines"

但是我想更改App.config(无需重新编译我的应用程序):

but i would like change in my App.config (without recompile my application):

GetStoredProcedureNameInAppConfig() //return "GetPerson"

"GetLines" 存储过程返回:

"GetLines" Stored Procedue return:

Instrument | Crystal
______________________
Instr1     | Crystal1
Instr1     | Crystal2
Instr2     | Crystal1

"GetPerson" 存储过程返回:

"GetPerson" Stored Procedue return:

FirstName | LastName | Language
_______________________________
Alex      | Doe      | ENG
Britt     | Nillis   | ENG
Carlo     | Petro    | FR

恢复
我想在通用" ObservableCollection中转换DataTable(具有可变结构). 参加特定课程.
(ObservableCollection对我来说不是一个好的解决方案.)

谢谢

Resume
I would like convert DataTable (with variable structure) in "generic" ObservableCollection. Witout specific class.
(ObservableCollection isn't good solution for me.)

Thank you

推荐答案

此解决方案可用于WPF绑定和筛选器.

This solution work with WPF Binding and Filter.

谢谢您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;
}

谢谢大家的帮助.

这篇关于将DataTable转换为ObservableCollection(没有特定的类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆