【问题标题】:Populate a List<T> and DataGridView with contents of an SqlDataReader使用 SqlDataReader 的内容填充 List<T> 和 DataGridView
【发布时间】:2012-06-11 19:24:33
【问题描述】:

我正在尝试编写一个可以接受三个参数的函数:SqlDataReader、DataGridView 和 List。

我想获取 SqlDataReader 的内容并创建一个对象列表,然后将其绑定到 DataGridView。

根据另一个 Stack Overflow 用户的一些建议,我得出以下结论:

public void FillArrayList<T>(DataGridView grid, SqlDataReader reader, List<T> list)
{
    //Fill the list with the contents of the reader
    while (reader.Read())
    {
        Object obj = new Object();
        Type type = typeof(T);

        FieldInfo[] fields = type.GetFields(); // Get the fields of the assembly
        int i = 0;

        foreach(var field in fields)
        {
            field.SetValue(obj, reader[i]); // set the fields of T to the reader's value
            i++;
        }

        list.Add((T)obj);
    }

    grid.DataSource = list;
}

运行代码时,将对象转换为类型 T 时出现错误:

无法将“System.Object”类型的对象转换为类型 'TestHarness.Organisation'。

我的印象是 Object 可以存储任何东西。谁能告诉我为什么无法执行此演员表?

谢谢,

安迪

【问题讨论】:

标签: c# list object datagridview sqldatareader


【解决方案1】:

您几乎可以将任何东西投射到 Object 上,但不能将 Object 投射到任何东西上。查看 MSDN 上的 System.Object 类。注意那里几乎什么都没有。演员表没有意义,因为它在功能上与调用新的 TestHarness.Organization 相同。

如果您确切知道要在 DataReader 中寻找什么以进入 TestHarness.Organization 或其他任何内容,您可以尝试user-defined conversion。这将允许您隐式或显式调用一些代码来为您进行类型更改,而无需任何额外的代码。

【讨论】:

  • 您好,感谢您的回复。我最初确实有一个采用 OrganisationList 和 OrganisationGrid 的方法。易于实现,但我需要一种非常相似的方法来用 JobList 的内容填充 JobGrid。我看到可以通过编写一个可以处理 anything 列表的方法来改进代码。是否可以将未知对象添加到通用 List
  • 不是这样的,不。您可以一般地声明一个 List 但出于编译目的,在您添加它时,所有内容都必须是一个 T,这不是很有用,因为您无法访问有关 T 的任何内容。我认为您实际上在寻找什么for 是interfaces 的一个很好的实现。如果你要让 OrgGrid 和 JobGrid 都实现接口 IWorkGrid 或任何你称之为的接口,那么你可以创建一个 List 并用它做你想做的事情。
【解决方案2】:

在 MMK 发布的链接的帮助下,在我的问题下方的评论中,我设计了一个解决方案:

public void FillList<T>(DataGridView grid, string SQLCommand, List<T> list) where T : class, new()
    {
        //Load the data into the SqlDataReader
        SqlCommand dataCommand = new SqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.CommandType = CommandType.Text;
        dataCommand.CommandText = SQLCommand;

        SqlDataReader dataReader = dataCommand.ExecuteReader();

        //Fill the list with the contents of the reader
        while (dataReader.Read())
        {
            var obj = new T();

            //Get the property information
            PropertyInfo[] properties = typeof(T).GetProperties();
            int i = 0;

            foreach(var property in properties)
            {
                property.SetValue((T)obj, dataReader[i], null); // set the fields of T to the reader's value
                i++;
            }

            list.Add(obj);
        }

        dataReader.Close();

        //Bind the list to the DataGridView
        grid.DataSource = list;
    }

似乎完全符合我的需要。当我在越来越多的情况下使用它时,我可能会遇到一些明显的错误,但是,这就是生活。

谢谢你们的帮助,伙计们!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多