Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fleemox - 18 Feb 2007 02:39 GMT
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
ClayB - 18 Feb 2007 08:53 GMT
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.datagridviewrowcol
lection.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.
Fleemox - 18 Feb 2007 21:15 GMT
The original thread (that first was posted in the wrong forum) can be
found at
http://groups.google.com/group/microsoft.public.vc.mfc/browse_frm/thread/cc26167
90ec97865/?hl=eng


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.