How do I populate a DataGridView with arrays containing strings? (I get convertion error C2664)

  • Thread starter Thread starter Fleemox
  • Start date Start date
F

Fleemox

This is my code (only the important parts of it):

int Rownumber = 1;
string Rowtext[8000];
DataGridView1->Rows->Add(Rowtext[Rownumber]);

This code (here simplyfied) should create a row in the grid and fill
the first cell with the text stored in the array Rowtext[1] but when
debugging I get the following error message:

error C2664: 'int
System::Windows::Forms::DataGridViewRowCollection::Add(...cli::array<Type,d­
imension>
^)' : cannot convert parameter 1 from 'std::string' to
'System::Object^'


If I change the code to

DataGridView1->Rows->Add("dummytext");

it WILL put the dummytext into a cell.
It doesn't work if I first declare/assign a string like this:

string strDummy = "dummytext";

and then write

DataGridView1->Rows->Add(strDummy);

I guess the Add() function can't accept a string as the argument
(only
if the string is created in advance, inside quotemarks).


What is the solution for this problem? I have found a few links which
could
help me, but I'm a newbie and those solutions look too complicated
for
me. I just want to get rid of the error, but not by refactoring the
whole
code.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagri...
http://support.microsoft.com/kb/816146
 
The class reference on the DataGriviewRowCollection.Add shows 4
overloads, none of which accepts a simple string.
http://msdn2.microsoft.com/en-au/library/system.windows.forms.datagridviewrowcollection.add.aspx

But one does accept an object array, so you should be able to pass it
a string array.

I am not familiar with managed C++, but here are two things that jump
out at me from your code.

1) Does

int Rownumber = 1;
string Rowtext[8000];

actually create the array or just declare. In C#, the code would be

int Rownumber = 1;
string[] Rowtext = new string[8000];

Notice the new operator that actually creates an instance of the
array.

2) The item you are passing to the Add method is a single string (with
what a suspect is a too large index since I am alomost sure arrays are
zero-based in Managed CPP). Maybe what you want to pass is just the
array name (again, not sure of the syntax)

DataGridView1->Rows->Add(Rowtext);

In C#, the code would be:

DataGridView1.Rows.Add(Rowtext);

==================
Clay Burch
Syncfusion, Inc.
 
Back
Top