0
votes

I am looking for an out of the box way to have a GridView on a form bound to a BindList that gets updated often and maintain the current entries.

I want to show some client objects in the GridView. Every few seconds my form reads an xml file which has the up-to-date information on the client (Name, variableNumber, variableNumber, state). I want to be able to update the information in the grid without destroying selection.

Can this be done out of the box?

1
What have you actually tried and or researched..? a simple google search might help Binding a DataGridView to a Collection - MethodMan
Can this be done out of the box? Yes - sa_ddam213

1 Answers

0
votes

Credit will go to DJ KRAZE if he moves his comment to an answer. While I had researched how to accomplish this nothing I found directly said that it would maintain selection and such.

My current solution is

using (var reader = new StreamReader(new FileStream(clientFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                var ser = new XmlSerializer(typeof(ClientList));
                var temp = (ClientList)ser.Deserialize(reader);

                foreach (var client in temp.Items)
                {
                    if(clientWatchers.Any(c => c.Handle == client.Handle))
                    {
                        var c = clientWatchers.First(w => w.Handle == client.Handle);
                        c.SyncNumber = client.SyncNumber;
                        c.PsNumber = client.PsNumber;
                        c.IsHalted = client.IsHalted;
                    }
                    else
                    {
                        clientWatchers.Add(client);
                    }
                }

                var handles = clientWatchers.Select(c => c.Handle).ToArray();

                foreach (var handle in handles)
                {
                    if(temp.Items.All(c => c.Handle != handle))
                        clientWatchers.Remove(clientWatchers.First(c => c.Handle == handle));
                }

                //foreach (var client in temp.Items)
                    //clientWatchers.Add(client);
                dgClientView.DataSource = clientWatchers;

            }