繁体   English   中英

当数据源 object 属性更改时更新 datagridview C#

[英]Updating a datagridview when a datasource object property is changed C#

我有这样的程序 class :

class Program
{
    public static UDProtocol MyUdp;
    private static readonly int Port = 11000;
    public static F_Main F1;
    public static BindingSource DtgvSource;


    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        OnStart();
        Application.Run();
    }

    private static void OnStart()
    {
        MyUdp = new UDProtocol(Port);
        F1 = new F_Main();

        MyUdp.Start();

        DtgvSource = new BindingSource(MyUdp.ClientList, null);
        F1.DTGV_Clients.DataSource = DtgvSource;
    }
}

我的 UDProtocol 看起来像这样(简化):

public class UDProtocol
{
    private readonly UdpClient UdpMe;
    private readonly int Port;
    private Client CurrentClient;
    public BindingList<Client> ClientList { get; set; }

    public UDProtocol(int _port)
    {
        Port = _port;
        UdpMe = new UdpClient(Port);
        ClientList = new BindingList<Client>();
    }

    public void Start()
    {
        ThrFlag = true;
        new Thread(() =>
        {
            while (ThrFlag)
            {
                if (UdpMe.Available > 0)
                {
                    try
                    {
                        NewClientEP = new IPEndPoint(IPAddress.Any, Port);
                        string data = Encoding.UTF8.GetString(UdpMe.Receive(ref NewClientEP));
                        CurrentClient = new Client
                        {
                            Name = Dns.GetHostEntry(NewClientEP.Address).HostName,
                            IP = NewClientEP.Address,
                        };

                        MsgObj NewMsg = new MsgObj(data) { From = CurrentClient };
                        AnalyseMessage(NewMsg);
                    }
                    catch { }
                }
                Thread.Sleep(1);
            }
        }).Start();
    }


    public void AnalyseMessage(MsgObj msg)
    {
            //some useless code
            msg.From.Connected = true;
    }
}

我还有一个 MsgObj class:

public class MsgObj
    {
        //some others variables
        private Client _From {get; set;}

        public MsgObj(string data)
        {
            //some useless code
        }
    }

以及实现INotifyPropertyChanged的客户端 class

    public class Client : INotifyPropertyChanged
    {
        public IPAddress IP { get; set; }
        private bool _connected;

        public event PropertyChangedEventHandler PropertyChanged;

        public bool Connected
        {
            get => _connected;

            set
            {
                _connected = value;
                NotifyPropertyChanged("Connected");
            }
        }

        private void NotifyPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }

在我的 F1 (F_Main) 中,我的 Datagridview 是这样构建的:

    public F_Main()
    {
        InitializeComponent();

        DTGV_Clients.AutoGenerateColumns = false;
        DTGV_Clients.Columns[0].DataPropertyName = "Connected";
        DTGV_Clients.Columns[1].DataPropertyName = "IP";
    }

当我将客户端添加到 ClientList (BindingList) 时,datagridview 会更新。 另一方面,当我尝试修改客户端的“已连接”属性时,尽管我实现了INotifyPropertyChanged接口,但它并未在 Datagridview 中更新。

我也尝试过使用ResetBinding(false)

分析数据()

但没有成功。

尽管我对这个主题进行了所有研究,但我看不出我做错了什么以及它可能来自哪里。

我试图更改与列表中的客户端不同的currentclient 所以我输入了AnalyzeData()

Client ThisClient = ClientList.FirstOrDefault(x => x.Name.Equals(msg.From.Name));
ThisClient.Connected = true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM