Luv L

Luv L

  • 1.5k
  • 177
  • 9k

open bracket is not apply in textbox from gridview input

Jan 30 2025 10:45 AM

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

private void UpdateTextBox()
{
    List<string> allLines = new List<string>();
    List<(int StartIndex, int EndIndex, int Multiplier)> multiplierGroups = new List<(int, int, int)>();
    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 < 8; j++)
        {
            if (row.Cells[j].Style.BackColor == Color.Black)
            {
                indexes.Append((j + 1) + ".");
            }
        }
        if (indexes.Length > 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);
}

 


Answers (1)