0

我正在使用 Interactive Brokers C# API 并尝试从AccountSummary对象创建数据表,但表中的行被覆盖并且只显示最后一个值。

我不知道发生了什么事。我认为可能是属性类型与列名匹配,因此它覆盖在同一行的顶部。

预期产出

在此处输入图像描述

public static DataTable ConverToDataTable(object obj)
{
    DataTable dt = new DataTable();
    DataRow dr = dt.NewRow();

    dt.Rows.Add(dr);

    obj.GetType().GetProperties().ToList().ForEach(f =>
    {
        try
        {
            f.GetValue(obj, null);
            dt.Columns.Add(f.Name, f.PropertyType);
            dt.Rows[0][f.Name] = f.GetValue(obj, null);
        }
        catch
        {

        }
    });
    return dt;
}

这是调用 ConvertToDataTable() 方法的方法。

public void UpdateUI(IBMessage message)
    {
        switch (message.Type)
        {
            case MessageType.AccountSummary:                    
                ConverToDataTable((AccountSummaryMessage)message);
                break;
            case MessageType.AccountSummaryEnd:
                HandleAccountSummaryEnd();
                break;
            case MessageType.AccountValue:
                HandleAccountValue((AccountValueMessage)message);
                break;
            case MessageType.PortfolioValue:
                HandlePortfolioValue((UpdatePortfolioMessage)message);
                break;
            case MessageType.AccountDownloadEnd:
                break;
            case MessageType.Position:
                HandlePosition((PositionMessage)message);
                break;
            case MessageType.PositionEnd:
                break;
        }
    }

这是我试图从中构建数据表的对象。

public class AccountSummaryMessage : IBMessage
{
    private int requestId;
    private string account;
    private string tag;
    private string value;
    private string currency;

    public int RequestId
    {
        get { return requestId; }
        set { requestId = value; }
    }

    public string Account
    {
        get { return account; }
        set { account = value; }
    }

    public string Tag
    {
        get { return tag; }
        set { tag = value; }
    }

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    public string Currency
    {
        get { return currency; }
        set { currency = value; }
    }

    public AccountSummaryMessage(int requestId, string account, string tag, string value, string currency)
    {
        Type = MessageType.AccountSummary;
        RequestId = requestId;
        Account = account;
        Tag = tag;
        Value = value;
        Currency = currency;
    }
}
4

2 回答 2

1

在添加行之前,您应该首先循环并将列添加到表中。如果您只是想在函数中创建一个包含单行的表,ConverToDataTable则需要将其更改为类似于以下内容:

public static DataTable ConverToDataTable(object obj)
{
    DataTable dt = new DataTable();

    var dataType = obj.GetType();

    dataType.GetProperties().ToList().ForEach(f =>
    {
        try
        {
            dt.Columns.Add(f.Name, f.PropertyType);
        }
        catch
        {

        }
    });

    DataRow newRow = dt.NewRow();
    foreach (var prop in dataType.GetProperties())
    {
        newRow[prop.Name] = prop.GetValue(obj);
    }
    dt.Rows.Add(newRow);

    return dt;
}

编辑:要添加多行,您有几个选项(正如我在下面的评论中解释的那样。这里有两种可能的方法:

在您的类中使用私有表变量以在需要时继续追加行

private DataDate table = null;

public void ConverToDataTable(object obj)
{
    var dataType = obj.GetType();

    if (table == null)
    {
        table = new DataTable();

        dataType.GetProperties().ToList().ForEach(f =>
        {
                table.Columns.Add(f.Name, f.PropertyType);
        });
    }

    DataRow newRow = table.NewRow();
    foreach (var prop in dataType.GetProperties())
    {
        newRow[prop.Name] = prop.GetValue(obj);
    }
    table.Rows.Add(newRow);
}

使用可枚举类型一次添加所有行

public static DataTable ConverToDataTable(IEnumerable<object> obj)
{
    DataTable dt = new DataTable();

    var dataType = obj.First().GetType();

    dataType.GetProperties().ToList().ForEach(f =>
    {
        try
        {
            dt.Columns.Add(f.Name, f.PropertyType);
        }
        catch
        {

        }
    });

    foreach (var item in obj)
    {
        DataRow newRow = dt.NewRow();
        foreach (var prop in dataType.GetProperties())
        {
            newRow[prop.Name] = prop.GetValue(item);
        }
        dt.Rows.Add(newRow);
    }

    return dt;
}

阅读可枚举类型可能会有所帮助。是一个很好的起点。

于 2017-10-05T01:04:58.597 回答
0

我使用了凯文的第一个编辑,它完美无缺。

于 2017-10-06T20:49:35.720 回答