Reputation: 55
I have a program coded in c# that receives UDP packets from different sources with multiple IP address.
I have created a datatable that store all those IP addresses and informations linked to the sources, and I try to display this table in the program when I run it.
Because the program is constantly listening to UDP packets, the viewing of the table should be updated in real time.
I have searched for Datagridview, but I didn't success to use it.
I would like to show on screen in a very simple way something like this : Data Viewing
static void Main(string[] args)
{
DataTable CommunicationTable = new DataTable();
initDataTableCommunication(CommunicationTable);
senderIdentifier SmartphoneTest = new senderIdentifier();
SmartphoneTest.addressIP = "192.120.120.0";
SmartphoneTest.message = "Started";
SmartphoneTest.port = 11000;
newEntryDateTableCom(SmartphoneTest, CommunicationTable);
senderIdentifier SmartphoneTest2 = new senderIdentifier();
SmartphoneTest2.addressIP = "192.120.45.9";
SmartphoneTest2.message = "Done";
SmartphoneTest2.port = 11000;
newEntryDateTableCom(SmartphoneTest2, CommunicationTable);
Here I fulfilled "manually" the DataTable, but the new entries will be created by receiving the UDP Packets
For the moment, I can only visualize the DataTable with the Debug, using the "scope" on the watch of the DataTable (Visual Studio)
Sorry for my poor English and thanks in advance
Upvotes: 1
Views: 3409
Reputation: 9650
You've got to create a new Windows Forms project and drop a DataGridView
control on it. Define your CommunicationTable
as a field of your newly created form and put your CommunicationTable
initialization somewhere in the initialization code (the form's constructor is a good candidate). This initialization should also set the DataSource
property of your DataGridview
to CommunicationTable
.
Then run your UDP listening routine in a separate thread and make it update the CommunicationTable
. But don't forget to use Form.Invoke()
to update data in the GUI thread.
Here is a simplified example:
DataTable CommunicationTable = new DataTable();
Thread listeningThread;
public Form1()
{
InitializeComponent();
CommunicationTable.Columns.Add("addressIP", typeof(string));
CommunicationTable.Columns.Add("port", typeof(int));
CommunicationTable.Rows.Add("127.0.0.1", 1100);
// Notice this assignment:
dataGridView1.DataSource = CommunicationTable;
listeningThread = new Thread(() => {
// UDP listener emulator.
// Replace with actual code but don't forget the Invoke()
for (int i = 0; i < 10; i++)
{
Invoke((MethodInvoker)delegate {
CommunicationTable.Rows.Add("127.0.0.1", 1024 + i); });
Thread.Sleep(300);
}
});
listeningThread.Start();
}
Upvotes: 0