2

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

Pbmke
  • 55
  • 2
  • 7
  • Are you really talking about showing the data in a console window or do you have a UI like windows forms and want to know how to work with a datagridview? For the first, the [`Console`](https://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx) class has a lot of methods to set cursor positions and colors, so you should be able to "draw" and update a table. The second version is a little too broad to answer here, but there are a lot of articles, how-tos and tutorials on the web. – René Vogt Aug 04 '16 at 11:40
  • Yes, I would prefer to create a UI for Windows instead of printing the same table again and again in the console. I am interested in working with a datagridview and a form, if it is possible with not too much code. – Pbmke Aug 04 '16 at 11:45
  • What did you assign to the `DataSource`property of your `DataGridview`? Was it a `DataTable` instance or some generic collection? – Dmitry Egorov Aug 04 '16 at 11:47
  • No I didn't, I just create a new datatable in my Main() : static void Main(string[] args) { DataTable CommunicationTable = new DataTable(); initDataTableCommunication(CommunicationTable); } Then the Table is filled automatically by receiving UDP packets – Pbmke Aug 04 '16 at 11:54
  • Could you please share the relevant code in the question? That may be helpful in tracking the issue down. – Dmitry Egorov Aug 04 '16 at 11:58
  • The Console can't update existing data/values, you have to print the table out every time in real time. the only way to update the value is use a UI such as WinForms, WPF, ASP etc. where you can update in real time individual columns/Cells – Softe Eng Aug 04 '16 at 12:07
  • Well, that's what I was thinking for the console, thanks ;) Is there a simple way to display a UI with WinForms/WPF/ASP ? – Pbmke Aug 04 '16 at 12:14
  • @Pbmke, I suggest to edit the question title and body to make it clear you've chosen to stick with a GUI application (according to your _"I would prefer to create a UI for Windows instead of printing the same table again and again in the console"_ statement). That would prevent further confusion. – Dmitry Egorov Aug 04 '16 at 12:18
  • An example using Datagridview in winforms from [Microsoft](https://msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.110).aspx). but that is not your question, if my answer below is useful to u, please mark it as an answer – Softe Eng Aug 04 '16 at 12:19
  • Please do warn next time before you change the question title, because the answer i provided was to a console app as your previous title stated. It would have gotten me down votes if i left it because you changed the title and therefor the answer is no longer useful! – Softe Eng Aug 04 '16 at 12:34
  • Yes, I am sorry, I just recently come to stack overflow – Pbmke Aug 04 '16 at 12:40

1 Answers1

1

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();
}
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • Thank you very much, I will try that solution, then mark it as an answer if it works – Pbmke Aug 04 '16 at 12:55
  • I do agree with your solution but i would have prefered to use **ThreadPool.QueueUserWorkItem** Or a **BackgroundWorker** :) – Softe Eng Aug 04 '16 at 12:56
  • @SofteEng, good point. Yes, the `ThreadPool.QueueUserWorkItem` is an option. Yet in this particular case I'd rather agree with http://stackoverflow.com/a/6193066/4295017 for we've got a _"persistent, dedicated, long-running thread"_. Anyway, both options are OK in our case and it's more a matter of programming taste which one to choose. – Dmitry Egorov Aug 04 '16 at 13:06
  • Thanks for your answers. How do you manage to drop the DataGridView on the the Windows Form Project? I don't find DataGridView in the items or user controls – Pbmke Aug 04 '16 at 13:52