0

How do I insert a value from a string into a dataGridView?

What I'm trying to do is to display a string from a 2D array who is returned by a method.

struct t_thing {
    std::string strings[3][3];
};
struct t_thing array = methodWith2DArrayStringReturn();
int index = dataGridView1->Rows->Add();
dataGridView1->Rows[index]->Cells[1]->Value = array.strings[0][0];

Error: E1767 function"System::Windows::Forms::BindingSource::DataSource::set" cannot be called with the given argument list CppCLR_WinForms_GUI argument types are: (std::string [20][3]).
object type is: System::Windows::Forms::BindingSource ^

I know the following works:

int index = dataGridView1->Rows->Add();
dataGridView1->Rows[index]->Cells[1]->Value = "word";
Ahmad.s
  • 33
  • 4
  • I think you should use System::String instead of std::string. – Ivan Venkov Jan 14 '23 at 19:58
  • But is there a way to create a 2D array as System::String^? Tells me array of handles is not allowed when swapping std::string for System::String^ – Ahmad.s Jan 14 '23 at 20:43
  • Check [this](https://stackoverflow.com/questions/995434/arrays-of-strings-in-managed-c). You can declare it as `array^>^` – Ivan Venkov Jan 14 '23 at 21:44
  • If I use that I cant use struct t_thing, because it says a member of a non managed class cannot be a handle when declaring it inside of it. And how do I declare the number of columns? This one doesent work: array^>^ test = gcnew array^>(3)(3); – Ahmad.s Jan 14 '23 at 22:14
  • If it's possible you can avoid using `struct t_thing` and set `methodWith2DArrayStringReturn()`'s return type to be `array^>`. Also when you declare two-dimensional array, you must initialize each of its members: `array^>^ test = gcnew array^>(3); test[0] = gcnew array(3);` and etc with 1 and 2. – Ivan Venkov Jan 15 '23 at 10:06

0 Answers0