hi,
please refer image gridview data display to textbox correctly,but only open [ btracket is not appluy in between the group .actual result should be below with open bracket
[1.3
2.4
1.3
2.4]4
1.3
2.4
but in image please refer in testbox result [ bracket is not added.

here my code
C#
private void UpdateTextBox()
{
List allLines = new List();
List multiplierGroups = new List();
int? currentGroupStart = null;
int currentMultiplier = 1;
// First collect all patterns from bottom to top
for (int i = dataGridView1.RowCount - 1; i >= 0; i--)
{
var row = dataGridView1.Rows[i];
StringBuilder indexes = new StringBuilder();
// Collect black cell indexes
for (int j = 0; j 0)
{
indexes.Length--; // Remove last dot
string line = indexes.ToString();
// Get multiplier value
int multiplier = int.TryParse(row.Cells[8].Value?.ToString(), out int val) ? val : 1;
if (multiplier > 1)
{
// Start new group
if (!currentGroupStart.HasValue)
{
currentGroupStart = allLines.Count;
currentMultiplier = multiplier;
}
}
else if (currentGroupStart.HasValue)
{
// End current group before adding the current line
multiplierGroups.Add((currentGroupStart.Value, allLines.Count - 1, currentMultiplier));
currentGroupStart = null;
}
allLines.Add(line);
}
}
// Handle any remaining open group at the end of processing
if (currentGroupStart.HasValue)
{
multiplierGroups.Add((currentGroupStart.Value, allLines.Count - 1, currentMultiplier));
}
// Process groups from last to first to maintain correct indexing
for (int i = multiplierGroups.Count - 1; i >= 0; i--)
{
var group = multiplierGroups[i];
// Get all lines in the group
string firstLine = allLines[group.StartIndex];
string lastLine = allLines[group.EndIndex];
// Add opening bracket to the first line of the group
allLines[group.StartIndex] = $"[{firstLine}";
// Add closing bracket and multiplier to the last line of the group
allLines[group.EndIndex] = $"{lastLine}]{group.Multiplier}";
}
// Join the lines and set them to the TextBox
textBox1.Text = string.Join(Environment.NewLine, allLines);
}
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 30, 2025, 7:21 AM EST
To fix this issue, you can create a new list to store the modified lines.
private void UpdateTextBox() { List allLines = new List(); List multiplierGroups = new List(); int? currentGroupStart = null; int currentMultiplier = 1; // First collect all patterns from bottom to top for (int i = dataGridView1.RowCount - 1; i >= 0; i--) { var row = dataGridView1.Rows[i]; StringBuilder indexes = new StringBuilder(); // Collect black cell indexes for (int j = 0; j 0) { indexes.Length--; // Remove last dot string line = indexes.ToString(); // Get multiplier value int multiplier = int.TryParse(row.Cells[8].Value?.ToString(), out int val) ? val : 1; if (multiplier > 1) { // Start new group if (!currentGroupStart.HasValue) { currentGroupStart = allLines.Count; currentMultiplier = multiplier; } } else if (currentGroupStart.HasValue) { // End current group before adding the current line multiplierGroups.Add((currentGroupStart.Value, allLines.Count - 1, currentMultiplier)); currentGroupStart = null; } allLines.Add(line); } } // Handle any remaining open group at the end of processing if (currentGroupStart.HasValue) { multiplierGroups.Add((currentGroupStart.Value, allLines.Count - 1, currentMultiplier)); } // Process groups from last to first to maintain correct indexing List resultLines = new List(allLines); for (int i = multiplierGroups.Count - 1; i >= 0; i--) { var group = multiplierGroups[i]; // Get all lines in the group string firstLine = resultLines[group.StartIndex]; string lastLine = resultLines[group.EndIndex]; // Add opening bracket to the first line of the group resultLines[group.StartIndex] = $"[{firstLine}"; // Add closing bracket and multiplier to the last line of the group resultLines[group.EndIndex] = $"{lastLine}]{group.Multiplier}"; } // Join the lines and set them to the TextBox textBox1.Text = string.Join(Environment.NewLine, resultLines); }