【问题标题】:How to bind a ReadOnlyCollection<T> to DataGridView如何将 ReadOnlyCollection<T> 绑定到 DataGridView
【发布时间】:2017-10-27 16:58:32
【问题描述】:

我正在使用名为ClearSCADA 的施耐德产品,它允许您通过.Net 接口查询其专有的后端数据库。我已经使用这个查询成功地查询了数据库:

    SELECT
        "Id", "FullName", "Foreground", "Blink", "Background", "TypeDesc", "MemoryUsage"
    FROM
        "CCustomColour"
    ORDER BY
        "FullName" ASC

并将结果作为 QueryResult 返回给我:

    using System.Collections.ObjectModel;

    namespace ClearScada.Client.Advanced
    {
        //
        public class QueryResult
        {
            //
            // Summary:
            //     Gets the results of the query.
            public ReadOnlyCollection<QueryRow> Rows { get; }
            //
            // Summary:
            //     Gets the number of rows affected for a DML query.
            public int RowsAffected { get; }
            //
            // Summary:
            //     Gets the status of the query.
            public QueryStatus Status { get; }
        }
    }

我尝试通过几种不同的方式将行的ReadOnlyCollection 绑定到数据网格。

   dataGridViewResults.DataSource = this.MyQueryResult.Rows;

                BindingSource bs = new BindingSource();
                bs.DataSource = this.MyQueryResult.Rows;
                dataGridViewResults.DataSource = bs;

网格向我显示了一个仅显示行索引但没有数据的列:

这里是 QueryRow 类的定义供参考:

using System.Collections.ObjectModel;

namespace ClearScada.Client.Advanced
{
    //
    public class QueryRow
    {
        //
        public ReadOnlyCollection<object> Data { get; }
        //
        public int Index { get; }
    }
}

我创建了我的 QueryResult 实例的本地副本,以便我可以在 Visual Studio 的“本地”窗口中捕获它的视图以显示在这里:

我想要实现的是这样显示的视图:

您能帮我看看如何将这些值绑定到 DataGridView 吗?

【问题讨论】:

    标签: c# .net datagridview binding


    【解决方案1】:

    我确信一定有更好的方法,但这对我有用,方法是将数据传输到 DataTable,然后将其绑定到 DataGridView:

                    if (this.MyQueryStatus.Equals(QueryStatus.Succeeded))
                    {
                        #region IF Succeeded
                        #region Create Results DataTable
                        DataTable resultsTable = new DataTable("Results");
                        resultsTable.Columns.Add("Id");
                        resultsTable.Columns.Add("FullName");
                        resultsTable.Columns.Add("Foreground");
                        resultsTable.Columns.Add("Blink");
                        resultsTable.Columns.Add("Background");
                        resultsTable.Columns.Add("TypeDesc");
                        resultsTable.Columns.Add("MemoryUsage");
                        #endregion Create Results DataTable
    
                        foreach (var row in this.MyQueryResult.Rows)
                        {
                            #region ForEach Row
                            DataRow newRow = resultsTable.NewRow();
                            newRow["Id"] = row.Data[0].ToString();
                            newRow["FullName"] = row.Data[1].ToString();
                            newRow["Foreground"] = row.Data[2].ToString();
                            newRow["Blink"] = row.Data[3].ToString();
                            newRow["Background"] = row.Data[4].ToString();
                            newRow["TypeDesc"] = row.Data[5].ToString();
                            newRow["MemoryUsage"] = row.Data[6].ToString();
                            resultsTable.Rows.Add(newRow);
                            #endregion ForEach Row
                        }
    
                        dataGridViewResults.DataSource = resultsTable;
                        tabControlMain.SelectedTab = tabControlMain.TabPages["tabPageResults"];
                        #endregion IF Succeeded
                    }
    

    如果有人能解释如何将 ReadOnlyCollection 直接绑定到 DataGridView,我将不胜感激。

    【讨论】:

    • 你试过dataGridViewResults.DataSource = this.MyQueryResult.Rows.ToList()吗?
    • 感谢您的想法,我只是尝试实现此功能,但“this.MyQueryResult.Rows”没有 ToList 方法。它只公开了 2 个方法,CopyTo 和 ToString。 CopyTo 方法允许将行的 ReadOnlyCollection 复制到 QueryRow 对象的数组中。我尝试了这个,然后在结果数组上寻找 ToList 方法,但它又没有 ToList 方法。
    • List 构造函数而不是直接转换呢? List&lt;MyQueryResults&gt; mqr = new List&lt;MyQueryResults&gt;(this.MyQueryResults);?
    • 谢谢,试过了,但不高兴,错误文本是'Argument 1: cannot convert from 'ClearSCADA.Client.Advanced.QueryResult' to int'。构造函数有 3 个签名 'List()'、'List(int capacity)'、'List(IEnumerable collection)'。
    • 于是我尝试了 List mqr = new List(this.MyQueryResults.Rows);因为 Rows 被定义为 System.Collections.ObjectModel.ReadOnlyCollection。似乎是不费吹灰之力(对我而言),然后我收到错误“参数 1:无法从 'System.Collections.ObjectModel.ReadOnlyCollection' 转换为 int”。我超出了我的深度,这让我感到困惑,因为它似乎与第三个签名匹配:'List(IEnumerable collection)'。我仍然对此感到困惑。
    猜你喜欢
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2013-05-17
    • 2013-10-24
    • 2012-12-11
    • 1970-01-01
    • 2010-12-28
    相关资源
    最近更新 更多