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);
}
code
Assistant
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);
}
}
}
}
To adjust your code so that the row counts are based on the lengths of GeneralSettings_TextFormat.accor.actTbl[i] and GeneralSettings_TextFormat.accor.phcTbl[i], you need to ensure that you are creating the correct number of rows each time the form becomes visible. This involves using the lengths of the arrays in GeneralSettings_TextFormat.accor.actTbl and GeneralSettings_TextFormat.accor.phcTbl instead of the DataGridView rows. Here's how you can implement the necessary changes:
Steps:
In the FieldsInitialization method, make sure to clear the existing rows and columns of your DataTable objects before adding new data. This prevents duplication or leftover data each time the form becomes visible.
Use the lengths from the actTbl and phcTbl arrays to populate the DataTable.
Modified Code:
Here's the adjusted code with comments explaining the changes:
csharp
private void FieldsInitialization(ModBusExporterLinker.GeneralSettings_TextFormat GeneralSettings_TextFormat)
{
// Clear existing data
actTblDataTable.Rows.Clear();
actTblDataTable.Columns.Clear();
phcTblDataTable.Rows.Clear();
phcTblDataTable.Columns.Clear();
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];
// Initialize columns
AddColumnIfNotExists(actTblDataTable, "Диапазон", typeof(int));
for (int i = 0; i < GeneralSettings_TextFormat.accor.actTbl.Length; i++)
{
AddColumnIfNotExists(actTblDataTable, $"Канал {i}", typeof(float));
}
// Populate rows based on actTbl length
for (int k = 0; k < GeneralSettings_TextFormat.accor.actTbl.Max(tbl => tbl.Length); k++)
{
DataRow newRow = actTblDataTable.NewRow();
newRow["Диапазон"] = k;
for (int i = 0; i < GeneralSettings_TextFormat.accor.actTbl.Length; i++)
{
if (k < GeneralSettings_TextFormat.accor.actTbl[i].Length)
{
newRow[$"Канал {i}"] = GeneralSettings_TextFormat.accor.actTbl[i][k];
}
}
actTblDataTable.Rows.Add(newRow);
}
actTblDataGridView.DataSource = actTblDataTable;
// Initialize phcTbl data similarly
AddColumnIfNotExists(phcTblDataTable, "Диапазон", typeof(int));
for (int i = 0; i < GeneralSettings_TextFormat.accor.phcTbl.Length; i++)
{
AddColumnIfNotExists(phcTblDataTable, $"Канал {i}", typeof(uint));
}
for (int k = 0; k < GeneralSettings_TextFormat.accor.phcTbl.Max(tbl => tbl.Length); k++)
{
DataRow newRow = phcTblDataTable.NewRow();
newRow["Диапазон"] = k;
for (int i = 0; i < GeneralSettings_TextFormat.accor.phcTbl.Length; i++)
{
if (k < GeneralSettings_TextFormat.accor.phcTbl[i].Length)
{
newRow[$"Канал {i}"] = GeneralSettings_TextFormat.accor.phcTbl[i][k];
}
}
phcTblDataTable.Rows.Add(newRow);
}
phcTblDataGridView.DataSource = phcTblDataTable;
}
Key Changes:
The rows are cleared and columns are reset before adding new data each time the form becomes visible to avoid duplication.
The Rows.Add calls use the lengths of the arrays in actTbl and phcTbl to ensure correct row counts, using Max to handle potentially different lengths across channels.
This structure prevents incorrect data visualization or dimensional mismatch in your DataGridView.
This should alleviate the problem of incorrectly creating rows based on the current visible state of the form.