I have my Program class like that:
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):
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:
public class MsgObj
{
//some others variables
private Client _From {get; set;}
public MsgObj(string data)
{
//some useless code
}
}
And client class that implement INotifyPropertyChanged
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:
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. 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.
I also tried with ResetBinding(false)
in
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. So I put in AnalyzeData()
:
Client ThisClient = ClientList.FirstOrDefault(x => x.Name.Equals(msg.From.Name));
ThisClient.Connected = true;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.