Skip to content

Commit

Permalink
Refactor DataGridView.Methods to remove ArrayList usage (#8650)
Browse files Browse the repository at this point in the history
Refactor DataGridView.Methods to remove ArrayList
  • Loading branch information
elachlan authored Feb 16, 2023
1 parent c83c75f commit 2a17484
Showing 1 changed file with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ private bool AdjustFillingColumns()
int imposedWidthSum = 0; // total width of columns that don't have a flexible width.
int requiredWidthSum = 0; // total of the minimum widths of the visible auto filled columns.
float weightSum = 0F; // total of the weights of the visible auto filled columns.
ArrayList autoFillColumns = null;
List<DataGridViewColumn> autoFillColumns = null;
foreach (DataGridViewColumn dataGridViewColumn in Columns)
{
if (dataGridViewColumn.Visible)
Expand All @@ -937,7 +937,7 @@ private bool AdjustFillingColumns()
numVisibleFillColumns++;
requiredWidthSum += dataGridViewColumn.DesiredMinimumWidth > 0 ? dataGridViewColumn.DesiredMinimumWidth : dataGridViewColumn.MinimumWidth;
weightSum += dataGridViewColumn.FillWeight;
autoFillColumns ??= new ArrayList(Columns.Count);
autoFillColumns ??= new(Columns.Count);

autoFillColumns.Add(dataGridViewColumn);
}
Expand Down Expand Up @@ -981,7 +981,7 @@ private bool AdjustFillingColumns()
availableWidth = 0;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
int minimumWidth = dataGridViewColumn.DesiredMinimumWidth > 0 ? dataGridViewColumn.DesiredMinimumWidth : dataGridViewColumn.MinimumWidth;
if (dataGridViewColumn.Thickness != minimumWidth)
{
Expand All @@ -997,7 +997,7 @@ private bool AdjustFillingColumns()
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
// Make sure the UsedFillWeight correspond to the actual column width
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
dataGridViewColumn.UsedFillWeight = dataGridViewColumn.Width * weightSum / availableWidth;
}

Expand All @@ -1020,7 +1020,7 @@ private bool AdjustFillingColumns()
bool desiredWidthTooSmall = false;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
if (columnEntry == autoFillColumns.Count - 1)
{
dataGridViewColumn.DesiredFillWidth = availableWidth - usedWidth;
Expand Down Expand Up @@ -1048,7 +1048,7 @@ private bool AdjustFillingColumns()
float weightSumNoneMinimal = weightSum;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
if (dataGridViewColumn.DesiredFillWidth == -1)
{
int minimumWidth = dataGridViewColumn.DesiredMinimumWidth > 0 ? dataGridViewColumn.DesiredMinimumWidth : dataGridViewColumn.MinimumWidth;
Expand All @@ -1060,7 +1060,7 @@ private bool AdjustFillingColumns()

for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
if (dataGridViewColumn.DesiredFillWidth != -1)
{
dataGridViewColumn.UsedFillWeight = dataGridViewColumn.FillWeight * usedWeightSumNoneMinimal / weightSumNoneMinimal;
Expand All @@ -1073,7 +1073,7 @@ private bool AdjustFillingColumns()
// Each UsedFillWeight simply equals the FillWeight
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
dataGridViewColumn.UsedFillWeight = dataGridViewColumn.FillWeight;
}
}
Expand All @@ -1091,7 +1091,7 @@ private bool AdjustFillingColumns()
// Allocate additional width according to UsedFillWeight and FillWeight values
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
dataGridViewColumn.DesiredFillWidth = dataGridViewColumn.Width;
}

Expand All @@ -1102,7 +1102,7 @@ private bool AdjustFillingColumns()
bool minimumColumnExists = false;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
fillWeightRatioSum += dataGridViewColumn.FillWeight / dataGridViewColumn.UsedFillWeight;
if (dataGridViewColumn.DesiredFillWidth <= dataGridViewColumn.MinimumWidth)
{
Expand All @@ -1112,7 +1112,7 @@ private bool AdjustFillingColumns()

for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
if (gain == 0)
{
floatDesiredWidths[columnEntry] = _availableWidthForFillColumns * dataGridViewColumn.UsedFillWeight / weightSum;
Expand All @@ -1131,7 +1131,7 @@ private bool AdjustFillingColumns()

for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
dataGridViewColumn.UsedFillWeight = weightSum / availableWidth * floatDesiredWidths[columnEntry];
}
}
Expand All @@ -1142,7 +1142,7 @@ private bool AdjustFillingColumns()
int cumulatedWidthLoss = 0;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
dataGridViewColumn.DesiredFillWidth = dataGridViewColumn.Width;
}

Expand All @@ -1161,7 +1161,7 @@ private bool AdjustFillingColumns()
DataGridViewColumn mostDeservingDataGridViewColumn = null;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
if (dataGridViewColumn.DesiredFillWidth > dataGridViewColumn.MinimumWidth)
{
fillWeightRatio = dataGridViewColumn.UsedFillWeight / dataGridViewColumn.FillWeight;
Expand All @@ -1177,9 +1177,9 @@ private bool AdjustFillingColumns()
if (mostDeservingDataGridViewColumn is not null)
{
float floatDesiredWidth = (stepDownAvailableWidthForFillColumns * mostDeservingDataGridViewColumn.UsedFillWeight / weightSum) - widthLoss * mostDeservingDataGridViewColumn.UsedFillWeight / mostDeservingDataGridViewColumn.FillWeight / fillWeightRatioSum;
if (floatDesiredWidth < (float)mostDeservingDataGridViewColumn.MinimumWidth)
if (floatDesiredWidth < mostDeservingDataGridViewColumn.MinimumWidth)
{
floatDesiredWidth = (int)mostDeservingDataGridViewColumn.MinimumWidth;
floatDesiredWidth = mostDeservingDataGridViewColumn.MinimumWidth;
}

int oldDesiredWidth = mostDeservingDataGridViewColumn.DesiredFillWidth;
Expand All @@ -1198,7 +1198,7 @@ private bool AdjustFillingColumns()
stepDownAvailableWidthForFillColumns -= oldDesiredWidth - mostDeservingDataGridViewColumn.DesiredFillWidth;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
dataGridViewColumn.UsedFillWeight = weightSum / stepDownAvailableWidthForFillColumns * dataGridViewColumn.DesiredFillWidth;
}
}
Expand All @@ -1218,7 +1218,7 @@ private bool AdjustFillingColumns()
float weightSumDbg = 0F;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
weightSumDbg += dataGridViewColumn.UsedFillWeight;
}

Expand All @@ -1236,7 +1236,7 @@ private bool AdjustFillingColumns()
DataGridViewColumn mostDeservingDataGridViewColumn = null;
if (autoFillColumns.Count == 1)
{
mostDeservingDataGridViewColumn = (DataGridViewColumn)autoFillColumns[0];
mostDeservingDataGridViewColumn = autoFillColumns[0];
mostDeservingDataGridViewColumn.DesiredFillWidth = Math.Max(availableWidth - usedWidth, mostDeservingDataGridViewColumn.MinimumWidth);
autoFillColumns.Clear();
}
Expand All @@ -1245,7 +1245,7 @@ private bool AdjustFillingColumns()
float biggestWeightDiscrepancy = 0F, weightDiscrepancy;
for (columnEntry = 0; columnEntry < autoFillColumns.Count; columnEntry++)
{
DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)autoFillColumns[columnEntry];
DataGridViewColumn dataGridViewColumn = autoFillColumns[columnEntry];
weightDiscrepancy = Math.Abs(dataGridViewColumn.UsedFillWeight - dataGridViewColumn.FillWeight) / dataGridViewColumn.FillWeight;
if (weightDiscrepancy > biggestWeightDiscrepancy || mostDeservingDataGridViewColumn is null)
{
Expand Down

0 comments on commit 2a17484

Please sign in to comment.