简体   繁体   English

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

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

I have my Program class like that:我有这样的程序 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;
    }
}

My UDProtocol look like that (simplified):我的 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;
    }
}

I also have a MsgObj class:我还有一个 MsgObj class:

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

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

And client class that implement INotifyPropertyChanged以及实现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));
        }
    }

In My F1 (F_Main) my Datagridview is built like this:在我的 F1 (F_Main) 中,我的 Datagridview 是这样构建的:

    public F_Main()
    {
        InitializeComponent();

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

When I add a client to the ClientList (BindingList), the datagridview is updated.当我将客户端添加到 ClientList (BindingList) 时,datagridview 会更新。 On the other hand, when I try to modify the "Connected" property of a client, it is not updated in the Datagridview despite my implementation of the INotifyPropertyChanged interface.另一方面,当我尝试修改客户端的“已连接”属性时,尽管我实现了INotifyPropertyChanged接口,但它并未在 Datagridview 中更新。

I also tried with ResetBinding(false) in我也尝试过使用ResetBinding(false)

AnalyzeData()分析数据()

but without success.但没有成功。

I cannot see what I'm doing wrong and where it can come from, despite all my research on the subject.尽管我对这个主题进行了所有研究,但我看不出我做错了什么以及它可能来自哪里。

I was trying to change the currentclient which is different from the client in the list.我试图更改与列表中的客户端不同的currentclient So I put in AnalyzeData() :所以我输入了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