async Task EditIPAddressCell(NetworkDeviceItem editedNDI, DataGridViewCell editedCell, string proposedAddress)
        {
            string oldAddress = editedNDI.IpAddress;
            Task   task       = editedNDI.ChangeIPAddress(proposedAddress);

            try
            {
                await task.ConfigureAwait(true);
            }
            catch (ArgumentException ex)
            {
                editedCell.Value = oldAddress;
                txt_Status.AppendText(ex.Message + Environment.NewLine);
            }
            catch (EngineeringNotSupportedException ex)
            {
                editedCell.Value = oldAddress;

                txt_Status.AppendText(ex.Message + Environment.NewLine);
                Debug.WriteLine(ex.Message);
            }

            string newAddress = editedNDI.IpAddress;

            editedCell.Value = newAddress;
            if (editedNDI.IpAddress == proposedAddress)
            {
                txt_Status.AppendText($"Device \"{editedNDI.HMName}\" changed IP address from: {oldAddress} to: {newAddress}." +
                                      $"{Environment.NewLine}");
            }

            DeviceRowsHelper.MarkRepeatingIPs(deviceTable.Rows, dgv_IpAddress.Index, dgv_Mode.Index);
        }
        private void PopulateDeviceTable(SortBy sortingOrder, string searchText)
        {
            if (projectLevel == null)
            {
                Debug.WriteLine("Nothing to populate device table with.");
                return;
            }

            deviceTableAutoEdit = true;
            deviceTable.Rows.Clear();
            ID.Clear();
            DeviceRowsHelper.DeviceByRowID.Clear();

            if (String.IsNullOrWhiteSpace(searchText) | searchText.Length <= 2)
            {
                deviceTable.Rows.AddRange(GetMatchingRows(sortingOrder).ToArray());
            }
            else
            {
                deviceTable.Rows.AddRange(GetMatchingRows(sortingOrder, searchText).ToArray());
            }
            deviceTableAutoEdit = false;

            DeviceRowsHelper.MarkRepeatingIPs(deviceTable.Rows, dgv_IpAddress.Index, dgv_Mode.Index);
        }
        private DataGridViewRow UnusedDeviceRow(NetworkDeviceItem unusedDevice)
        {
            var unusedDeviceRow = DeviceRowsHelper.InitializeRow(ID.NextID, unusedDevice, null, deviceTable);
            var cells           = unusedDeviceRow.Cells;
            var deviceNameCell  = cells[dgv_DeviceName.Index];

            DeviceRowsHelper.PadCell(deviceNameCell, 5);
            DeviceRowsHelper.DisableCells(cells[dgv_IoSystem.Index], cells[dgv_PnSubnet.Index],
                                          cells[dgv_RouterAddress.Index], cells[dgv_Mask.Index],
                                          cells[dgv_PnNumber.Index]);

            return(unusedDeviceRow);
        }
        private DataGridViewRow IoControllerRow(IoSystemLevel ioSystem)
        {
            var ioControllerRow = DeviceRowsHelper.InitializeRow(ID.NextID, ioSystem.IoController, ioSystem, deviceTable);
            var cells           = ioControllerRow.Cells;
            var deviceNameCell  = cells[dgv_DeviceName.Index];

            DeviceRowsHelper.PadCell(deviceNameCell, 0);
            DeviceRowsHelper.DisableCell(cells[dgv_PnNumber.Index]);

            if (!ioSystem.IoController.UseRouter)
            {
                DeviceRowsHelper.DisableCell(cells[dgv_RouterAddress.Index]);
            }

            return(ioControllerRow);
        }
        private DataGridViewRow IoDeviceRow(NetworkDeviceItem ioDevice, IoSystemLevel ioSystem)
        {
            var ioDeviceRow    = DeviceRowsHelper.InitializeRow(ID.NextID, ioDevice, ioSystem, deviceTable);
            var cells          = ioDeviceRow.Cells;
            var deviceNameCell = cells[dgv_DeviceName.Index];

            DeviceRowsHelper.PadCell(deviceNameCell, 25);
            DeviceRowsHelper.DisableCells(cells[dgv_IoSystem.Index], cells[dgv_PnSubnet.Index],
                                          cells[dgv_RouterAddress.Index], cells[dgv_Mask.Index]);

            if (string.IsNullOrEmpty(cells[dgv_IpAddress.Index].Value?.ToString())) // empty in future?
            {
                DeviceRowsHelper.DisableCell(cells[dgv_IpAddress.Index]);
            }

            return(ioDeviceRow);
        }
        public static void MarkRepeatingIPs(DataGridViewRowCollection rows, int IPColumnInd, int ModeColumnInd)
        {
            foreach (DataGridViewRow row in rows)
            {
                DataGridViewCell cell = row.Cells[IPColumnInd];
                if (!string.IsNullOrEmpty(cell.Value.ToString()))
                {
                    cell.Style.BackColor = System.Drawing.SystemColors.Control;
                }
                else
                {
                    cell.Style.BackColor = System.Drawing.SystemColors.ControlLight;
                }
            }

            foreach (int index in DeviceRowsHelper.FindRepeatingIPs(rows, IPColumnInd, ModeColumnInd))
            {
                rows[index].Cells[IPColumnInd].Style.BackColor = System.Drawing.Color.Pink;
            }
        }