Weird Datagrid / paint behaviour

Posted by Shane.C on Stack Overflow See other posts from Stack Overflow or by Shane.C
Published on 2012-11-20T10:57:46Z Indexed on 2012/11/20 11:00 UTC
Read the original article Hit count: 206

Filed under:
|
|
|

The scenario:

A method sends out a broadcast packet, and returned packets that are validated are deemed okay to be added as a row in my datagrid (The returned packets are from devices i want to add into my program).

So for each packet returned, containing information about a device, i create a new row. This is done by first sending packets out, creating rows and adding them to a list of rows that are to be added, and then after 5 seconds (In which case all packets would have returned by then) i add the rows. Here's a few snippets of code.

Here for each returned packet, i create a row and add it to a list;

DataRow row = DGSource.NewRow();
                    row["Name"] = deviceName;
                    row["Model"] = deviceModel;
                    row["Location"] = deviceLocation;
                    row["IP"] = finishedIP;
                    row["MAC"] = finishedMac;
                    row["Subnet"] = finishedSubnet;
                    row["Gateway"] = finishedGateway;
                    rowsToAdd.Add(row);

Then when the timer elapses;

void timerToAddRows_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        timerToAddRows.Enabled = false;
        try
        {
            int count = 0;
            foreach (DataRow rowToAdd in rowsToAdd)
            {
                DGSource.Rows.Add(rowToAdd);
                count++;
            }
            rowsToAdd.Clear();

            DGAutoDevices.InvokeEx(f => DGAutoDevices.Refresh());
            lblNumberFound.InvokeEx(f => lblNumberFound.Text = count + " new devices found.");
        }
        catch
        {
        }
    }

So at this point, each row has been added, and i call the re paint, by doing refresh. (Note: i've also tried refreshing the form itself, no avail). However, when the datagrid shows the rows, the scroll bar / datagrid seems to have weird behavour..for example i can't highlight anything with clicks (It's set to full row selection), and the scroll bar looks like so;

enter image description here Calling refresh doesn't work, although if i resize the window even 1 pixel, or minimize and maximise, the problem is solved. Other things to note : The method that get's the packets and adds the rows to the list, and then from the list to the datagrid runs in it's own thread. Any ideas as to something i might be missing here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms