0

我有一个 WCF 客户端,我需要将 DTO 放入BindingSource中,但是当我尝试将过滤器应用于 BindingSource 时,它​​不能作为整洁的 List 或 BindingList 工作,因为它们都返回 false IBindingListView.SupportsFiltering

我需要使用什么数据类型才能过滤我的列表?

代理 DTO:

namespace MyApp.DataContracts
{
    [System.Runtime.Serialization.DataContractAttribute(Name = "ServerSetupDTO", Namespace = "http://example.com/MyApp")]
    [System.SerializableAttribute()]
    public partial class ServerSetupDTO : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.ComponentModel.BindingList<MyApp.DataContracts.DatabaseInfo> GenericPreconfigs
        {
            //(Snip)
        }

        //(Snip)
    }

    [System.Runtime.Serialization.DataContractAttribute(Name = "DatabaseInfo", Namespace = "http://example.com/MyApp")]
    [System.SerializableAttribute()]
    public partial class DatabaseInfo : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        [System.Runtime.Serialization.DataMemberAttribute()]
        public string AllowedServer
        {
            //(Snip)
        }

        //(Snip)
    }
}

应用过滤器:

ServerSetupDTO serverSetupDTO = proxyClient.GetServerSetup();
bsServerSettings.DataSource = serverSetupDTO ;

bsPreconfigList.DataMember = "GenericPreconfigs";
bsPreconfigList.DataSource = bsServerSettings;

//This filter never gets applied, bsPreconfigList.SupportsFiltering is false.
bsPreconfigList.Filter = String.Format("AllowedServer = '{0}'", cmbHost.Text); 
4

1 回答 1

0

扯掉头发后,我决定只制作一个 ToDataSet 函数,因为我永远不需要更新值。

public partial class ServerSetupDTO
{
    public DataSet ToDataSet()
    {

        var setupDataSet = new DataSet("ServerSetup");

        //Get the demos
        var demoPreconfigsTable = setupDataSet.Tables.Add("DemoPreconfigs");
        demoPreconfigsTable.Columns.Add("AllowedServer");
        demoPreconfigsTable.Columns.Add("SqlInstance");
        demoPreconfigsTable.Columns.Add("DatabaseName");
        this.DemoPreconfigs.ForEach(item => demoPreconfigsTable.Rows.Add(item.AllowedServer, item.SqlInstance, item.DatabaseName));

        //Get the preconfigs
        var genericPreconfigsTable = setupDataSet.Tables.Add("GenericPreconfigs");
        genericPreconfigsTable.Columns.Add("AllowedServer");
        genericPreconfigsTable.Columns.Add("SqlInstance");
        genericPreconfigsTable.Columns.Add("DatabaseName");
        this.GenericPreconfigs.ForEach(item => genericPreconfigsTable.Rows.Add(item.AllowedServer, item.SqlInstance, item.DatabaseName));

        //Get the servers
        var sqlServersTable = setupDataSet.Tables.Add("SqlServers");
        sqlServersTable.Columns.Add("ServerName");
        this.SqlServers.ForEach(item => sqlServersTable.Rows.Add(item));

        //Get the VM's
        var virtualMachinesTable = setupDataSet.Tables.Add("VirtualMachines");
        virtualMachinesTable.Columns.Add("MachineName");
        this.VirtualMachines.ForEach(item => virtualMachinesTable.Rows.Add(item));

        return setupDataSet;
    }
}

然后我只需将我的一行代码更新为此

bsServerSettings.DataSource = Program.ServerSettings.ToDataSet();
于 2013-02-22T19:00:54.690 回答