I have a form which creates a dynamic tables with data stored in GeneralSettings_TextFormat. The problem is that each time this form change visibility it tries to add some rows correlated to GeneralSettings_TextFormat. How to change the code that rows count was based on GeneralSettings_TextFormat.accor.actTbl[i].Length and GeneralSettings_TextFormat.accor.phcTbl[i].Length.

Code:
using ModBusHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ModBusHelper.ModBusExporterLinker;

namespace Configurator
{
    public partial class CorrectionSettingsForm : Form
    {
        DataTable actTblDataTable = new DataTable();
        DataTable phcTblDataTable = new DataTable();
        public CorrectionSettingsForm(ModBusExporterLinker.GeneralSettings_TextFormat GeneralSettings_TextFormat)
        {
            InitializeComponent();
            FieldsInitialization(GeneralSettings_TextFormat);
        }

        private void CorrectionSettingsForm_VisibleChanged(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                Database.GeneralSettings_TextFormat.accor.AdcOfs[0] = AdcOfsChnlATextBox.Text;
                Database.GeneralSettings_TextFormat.accor.AdcOfs[1] = AdcOfsChnlBTextBox.Text;
                Database.GeneralSettings_TextFormat.accor.AdcOfs[2] = AdcOfsChnlCTextBox.Text;
                Database.GeneralSettings_TextFormat.accor.AdcOfs[3] = AdcOfsChnlNTextBox.Text;

                Database.GeneralSettings_TextFormat.accor.actTbl = new float[(actTblDataGridView.Columns.Count - 1)][];

                for (int i = 0; i < (actTblDataGridView.Columns.Count - 1); i++)
                {
                    float[] actTbl_channel_instant_values = new float[actTblDataGridView.Rows.Count - 1];
                    for (int k = 0; k < (actTblDataGridView.Rows.Count - 1); k++)
                    {
                        actTbl_channel_instant_values[k] = (float)actTblDataTable.Rows[k][i + 1];
                    }
                    Database.GeneralSettings_TextFormat.accor.actTbl[i] = actTbl_channel_instant_values;
                }

                Database.GeneralSettings_TextFormat.accor.phcTbl = new uint[(phcTblDataGridView.Columns.Count - 1)][];

                for (int i = 0; i < (phcTblDataGridView.Columns.Count - 1); i++)
                {
                    uint[] phcTbl_channel_instant_values = new uint[phcTblDataGridView.Rows.Count - 1];
                    for (int k = 0; k < (phcTblDataGridView.Rows.Count - 1); k++)
                    {
                        phcTbl_channel_instant_values[k] = (uint)phcTblDataTable.Rows[k][i + 1];
                    }
                    Database.GeneralSettings_TextFormat.accor.phcTbl[i] = phcTbl_channel_instant_values;
                }
            }
            else
            {
                FieldsInitialization(Database.GeneralSettings_TextFormat);
            }
        }

        private void FieldsInitialization(ModBusExporterLinker.GeneralSettings_TextFormat GeneralSettings_TextFormat)
        {
            Validation.Validate(AdcOfsChnlATextBox, Validation.FloatNumbersOnly, Validation.MaxValueToFloat);
            Validation.Validate(AdcOfsChnlBTextBox, Validation.FloatNumbersOnly, Validation.MaxValueToFloat);
            Validation.Validate(AdcOfsChnlCTextBox, Validation.FloatNumbersOnly, Validation.MaxValueToFloat);
            Validation.Validate(AdcOfsChnlNTextBox, Validation.FloatNumbersOnly, Validation.MaxValueToFloat);

            AdcOfsChnlATextBox.Text = GeneralSettings_TextFormat.accor.AdcOfs[0];
            AdcOfsChnlBTextBox.Text = GeneralSettings_TextFormat.accor.AdcOfs[1];
            AdcOfsChnlCTextBox.Text = GeneralSettings_TextFormat.accor.AdcOfs[2];
            AdcOfsChnlNTextBox.Text = GeneralSettings_TextFormat.accor.AdcOfs[3];

            AddColumnIfNotExists(actTblDataTable, "Диапазон", typeof(int));

            for (int i = 0; i < GeneralSettings_TextFormat.accor.actTbl.Length; i++)
            {
                AddColumnIfNotExists(actTblDataTable, $"Канал {i}", typeof(float));

                if (i == 0)
                {
                    for (int k = 0; k < GeneralSettings_TextFormat.accor.actTbl[i].Length; k++)
                    {
                        actTblDataTable.Rows.Add(new object[] { k, GeneralSettings_TextFormat.accor.actTbl[i][k] });
                    }
                }

                else
                {
                    for (int k = 0; k < GeneralSettings_TextFormat.accor.actTbl[i].Length; k++)
                    {
                        actTblDataTable.Rows[k][i + 1] = GeneralSettings_TextFormat.accor.actTbl[i][k];
                    }
                }
            }

            actTblDataGridView.DataSource = actTblDataTable;

            AddColumnIfNotExists(phcTblDataTable, "Диапазон", typeof(int));

            for (int i = 0; i < GeneralSettings_TextFormat.accor.phcTbl.Length; i++)
            {
                AddColumnIfNotExists(phcTblDataTable, $"Канал {i}", typeof(uint));

                if (i == 0)
                {
                    for (int k = 0; k < GeneralSettings_TextFormat.accor.phcTbl[i].Length; k++)
                    {
                        phcTblDataTable.Rows.Add(new object[] { k, GeneralSettings_TextFormat.accor.phcTbl[i][k] });
                    }
                }

                else
                {
                    for (int k = 0; k < GeneralSettings_TextFormat.accor.phcTbl[i].Length; k++)
                    {
                        phcTblDataTable.Rows[k][i + 1] = GeneralSettings_TextFormat.accor.phcTbl[i][k];
                    }
                }
            }

            phcTblDataGridView.DataSource = phcTblDataTable;
        }

        private void AddColumnIfNotExists(DataTable table, string columnName, Type type)
        {
            if (!table.Columns.Contains(columnName))
            {
                table.Columns.Add(columnName, type);
            }
        }
    }
}