C++ 在数据网格视图中添加新行 table

C++ add new row in datagrid view table

这里使用visual studio。在下面的代码中,您可以看到一个字符串被拆分,我想将每个拆分字符串放在一个新行中,但是程序崩溃了,没有创建一个新行,我得到了错误:

Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

String^ text = textBox1->Text;
cli::array<String^>^ part = text->Split('.','?','!');
for (int split = 0; split < part->Length; ++split) 
{
    datagrid->Rows[split]->Cells[3]->Value = part[split];
}

您的数据网格中可能没有足够的行,因此您需要将它们添加到循环中:

String^ text = textBox1->Text;
cli::array<String^>^ part = text->Split('.','?','!');

datagrid->Rows->Clear();
for (int split = 0; split < part->Length; ++split) 
{
    datagrid->Rows->Add(part[split]);
}