Search code examples
winformsc++-cli

add a value to the end of the array array<String^>^ c++ winform


how to add a value to the end of the array part before dataGridView1->Rows->Add(part);

for (int i = 0; i <= rw->Length; i++)
                    {
                    array<String^>^ part = rw[i]->ToString()->Split(L',');
                        dataGridView1->Rows->Add(part);
                    }

value from countprodtextBox->Text;


Solution

  • You cannot modify the size of an array. But you can create a new array with an increased length, copy the original array over, and assign the last element.

    array<String^>^ copy = gcnew array<String^>(part->Length + 1);
    for (int j = 0; j < part->Length; ++j)
        copy[j] = part[j];
    copy[copy->Length-1] = ...
    

    Btw: your index i will run out of bounds. You have to replace <= by <.