Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

18 views
Skip to first unread message

Fleemox

unread,
Feb 17, 2007, 5:56:55 PM2/17/07
to
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,dimension>
^)' : 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 but 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
ready created inside quotemarks).

What is the solution for this? 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, not by refactoring the whole
code.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.columns.aspx
http://support.microsoft.com/kb/816146

Ajay Kalra

unread,
Feb 17, 2007, 8:34:43 PM2/17/07
to
This is all Winforms and not MFC. Post it in dotnet.vcplus and/or winforms
forum instead.

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


"Fleemox" <garante...@yahoo.se> wrote in message
news:1171753015.8...@j27g2000cwj.googlegroups.com...

Joseph M. Newcomer

unread,
Feb 18, 2007, 2:42:40 AM2/18/07
to
See below...

On 17 Feb 2007 14:56:55 -0800, "Fleemox" <garante...@yahoo.se> wrote:

>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,dimension>
>^)' : cannot convert parameter 1 from 'std::string' to 'System::Object
>^'

****
So what is the specified argument to the method in question? You seem to be assuming it
can be a 'string' data type, when in fact it is probably something else. Also, why do you
need to keep the row text if it is in the grid, and why is 8000 considered a meaningful
number?

Show us the definition of Add() and it should be obvious what is wrong, and it is quite
possibly obvious what the solution is. Since you didn't specify which of the fifty or
hundred forms of datagrid you are using, it is hard to guess.

I note that you are using managed code, but string is not a managed code object.
****


>
>
>If I change the code to
>
> DataGridView1->Rows->Add("dummytext");
>
>it WILL put the dummytext into a cell but 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
>ready created inside quotemarks).
>
>What is the solution for this? 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, not by refactoring the whole
>code.
>
>http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.columns.aspx
>http://support.microsoft.com/kb/816146

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Fleemox

unread,
Feb 18, 2007, 4:03:44 PM2/18/07
to garante...@yahoo.se
> Joseph M. Newcomer wrote:
>
> So what is the specified argument to the method in question? You seem to be assuming it
> can be a 'string' data type, when in fact it is probably something else.

Yes, maybe it can't be a 'string' data type. I just thought that I
could throw in whatever data or datacontaining thing inside the
parenthesis of the Add() function and then get it to show up in the
grid, but that is probably not how it works.

> Also, why do you
> need to keep the row text if it is in the grid, and why is 8000 considered a meaningful
> number?

The main task for the final software that I'm creating is to import a
text file containing about 8000 bird observations written in a
unconsistent way and to transform it into a better organized CSV file
or any other file format that can be inported into Excel. The grid is
only there temporarily for me so that I can check that I'm creating
the string manipulating algorithms in the right way. Perhaps I should
import the text file directly into the grid, without going through the
arrays? I have no idea what is the best way to do it. The DataGridView
is a totally new thing for me.

> Show us the definition of Add() and it should be obvious what is wrong, and it is quite
> possibly obvious what the solution is.

I'm not sure that I know how to do that. Perhaps I can get to the
definition by right-clicking on the word Add() in the code and then
choose "Go to definition". Then I get four different rows to choose
from. This one seems to be the only one of them that also adds
contents to the row that is being created in the grid:

-----
System.Int32 Add(System.Object[] values)
Member of System.Windows.Forms.DataGridViewRowCollection

Summary:
Adds a new row to the collection, and populates the cells with the
specified objects.

Parameters:
values: A variable number of objects that populate the cells of the
new System.Windows.Forms.DataGridViewRow.

Return Values:
The index of the new row.

Exceptions:
System.InvalidOperationException: The associated
System.Windows.Forms.DataGridView control is performing one of the
following actions that temporarily prevents new rows from being
added:Selecting all cells in the control.Clearing the selection.-or-
This method is being called from a handler for one of the following
System.Windows.Forms.DataGridView
events:System.Windows.Forms.DataGridView.CellEnterSystem.Windows.Forms.DataGridView.CellLeaveSystem.Windows.Forms.DataGridView.CellValidatingSystem.Windows.Forms.DataGridView.CellValidatedSystem.Windows.Forms.DataGridView.RowEnterSystem.Windows.Forms.DataGridView.RowLeaveSystem.Windows.Forms.DataGridView.RowValidatedSystem.Windows.Forms.DataGridView.RowValidating-
or-The System.Windows.Forms.DataGridView.VirtualMode property of the
System.Windows.Forms.DataGridView is set to true.- or -The
System.Windows.Forms.DataGridView.DataSource property of the
System.Windows.Forms.DataGridView is not null.-or-The
System.Windows.Forms.DataGridView has no columns. -or-The row returned
by the System.Windows.Forms.DataGridView.RowTemplate property has more
cells than there are columns in the control.-or-This operation would
add a frozen row after unfrozen rows.
System.ArgumentNullException: values is null.

Attributes:
[System.ComponentModel.DesignerSerializationVisibilityAttribute(0)]
-----

> Since you didn't specify which of the fifty or
> hundred forms of datagrid you are using, it is hard to guess.

Oh, I didn't know about any other than "DataGridView" and the old
"DataGrid" that I found in the Toolbox for C++ (WinForms project).

Fleemox

unread,
Feb 18, 2007, 4:08:51 PM2/18/07
to garante...@yahoo.se
By the way, Ajay Kalra told me to post the question in another forum
so I did move this discussion to microsoft.­public.­dotnet.­framework.­
windowsforms.

David Ching

unread,
Feb 18, 2007, 11:08:50 PM2/18/07
to
"Fleemox" <garante...@yahoo.se> wrote in message
news:1171753015.8...@j27g2000cwj.googlegroups.com...
>
> I guess the Add() function can't accept a string as the argument (only
> ready created inside quotemarks).
>
> What is the solution for this? 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, not by refactoring the whole
> code.
>

This is a C++/CLI question. The best newsgroup for this is
microsoft.public.dotnet.languages.vc.

Having said that, I guess C++/CLI is no more "off topic" to MFC than a wide
variety of questions that get posted here. I believe if you change the line
to:

DataGridView1->Rows->Add(strDummy.c_str());

then it will work. System::String() has a constructor that takes a UNICODE
string.

-- David (MVP)

0 new messages