Luv L

Luv L

  • 1.5k
  • 175
  • 8.9k

add data from textbox input to gridview c# winform

Jan 28 2025 2:14 PM

hi below is my for i want to add textbox input to gridview
below code is before bracket it will added all data added correctly.but if using bracket then not added 
input in textbox is
8-A

4-B

12-A

[2-D

6-C]5

8-A

 private void textBox11_TextChanged(object sender, EventArgs e)
 {
      string input = textBox11.Text;
      ProcessInputAndUpdateGrid(input);

 }
 
 //
 private void ProcessInputAndUpdateGrid(string input)
 {
     // Clear previous data
     dataGridView1.Columns.Clear();
     dataGridView1.Rows.Clear();

     // Initialize grid with "Column1" and first row "X"
     dataGridView1.Columns.Add("Column1", "Column1");
     dataGridView1.Rows.Add("X");

     Dictionary<string, int> rowMap = new Dictionary<string, int>();
     string lastColumnName = "";

     bool inBracket = false; // Track if we're inside a bracket
     int currentMultiplier = 1; // Default multiplier
     string bracketContent = ""; // Store content inside the bracket

     // Split input into lines
     var lines = input.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

     foreach (var line in lines)
     {
         string trimmedLine = line.Trim();

         if (trimmedLine.StartsWith("["))
         {
             // We have encountered a bracketed section
             inBracket = true;
             bracketContent = trimmedLine.Substring(1).Trim(); // Collect content inside the bracket
         }
         else if (trimmedLine.EndsWith("]"))
         {
             // We are closing the bracket
             inBracket = false;
             bracketContent += "\n" + trimmedLine.Substring(0, trimmedLine.Length - 1).Trim(); // Add this line to bracket content

             // Parse multiplier after closing bracket (if available)
             string afterBracketContent = trimmedLine.Substring(trimmedLine.IndexOf(']') + 1).Trim();
             if (!string.IsNullOrEmpty(afterBracketContent) && !int.TryParse(afterBracketContent, out currentMultiplier))
             {
                 currentMultiplier = 1; // If no valid multiplier is found, use default multiplier (1)
             }

             // Process the bracket content with the correct multiplier
             ProcessBracketContent(bracketContent, currentMultiplier, rowMap, ref lastColumnName);

             // Reset bracket content for future use
             bracketContent = "";
         }
         else if (inBracket)
         {
             // We're inside the bracket, collect content until we encounter closing bracket
             bracketContent += "\n" + trimmedLine;
         }
         else
         {
             // Process regular lines (outside bracket) with default multiplier 1
             ProcessItem(trimmedLine, rowMap, 1, ref lastColumnName); // Default multiplier 1 for non-bracketed lines
         }
     }

     // Apply default multiplier of 1 to the last column in the X row
     if (!string.IsNullOrEmpty(lastColumnName))
     {
         if (dataGridView1.Rows[0].Cells[lastColumnName].Value == null)
         {
             dataGridView1.Rows[0].Cells[lastColumnName].Value = 1;
         }
     }
 }

 private void ProcessBracketContent(string bracketContent, int multiplier, Dictionary<string, int> rowMap, ref string lastColumnName)
 {
     // Split bracket content into items and process them
     var items = bracketContent.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
     foreach (var item in items)
     {
         ProcessItem(item.Trim(), rowMap, multiplier, ref lastColumnName); // Process each item with the correct multiplier
     }
 }

 private void ProcessItem(string item, Dictionary<string, int> rowMap, int multiplier, ref string lastColumnName)
 {
     // Parse value and key from input like "2-A"
     var parts = item.Split('-');
     if (parts.Length != 2) return;

     if (!int.TryParse(parts[0], out int value))
     {
         MessageBox.Show("Error: Invalid value in input.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     string key = parts[1];

     // Add row for the key if not exists
     if (!rowMap.ContainsKey(key))
     {
         rowMap[key] = dataGridView1.Rows.Add(key);
     }

     // Determine column dynamically
     string columnName = "C" + (2 + dataGridView1.Columns.Count - 1); // Start from C2 dynamically
     if (!dataGridView1.Columns.Contains(columnName))
     {
         dataGridView1.Columns.Add(columnName, columnName);
     }

     // Assign value to the corresponding cell
     dataGridView1.Rows[rowMap[key]].Cells[columnName].Value = value;

     // Assign multiplier to X row (if applicable)
     if (multiplier > 1)
     {
         dataGridView1.Rows[0].Cells[columnName].Value = multiplier;
     }

     // Track the last column name for future processing
     lastColumnName = columnName;
 }

 


Answers (1)