Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > DataGridView Column Error Issue
 

DataGridView Column Error Issue

Hi,

Why do I get the below error...

Column named DataGridViewTextBoxColumn { Name=DataGridViewImageColumn { Name=NewsColumn1, Index=-1 }, Index=1 } cannot be found.

Parameter name: columnName

Here's my code to add a column...

Code Snippet

DataGridViewImageColumn myColumnName = new DataGridViewImageColumn();

ColumnName.Name = myColumnName + "1";

myDataGridView.Columns.Add(myColumnName.ToString(), myColumnName.ToString());

Then I want to add images to each cell...

Get the error in the red line.

Code Snippet

// INITIALISE DGV

foreach (DataGridViewColumn myCol in myDataGridView.Columns)

{

for (int i = 0; i < this.myDataGridView.RowCount; i++)

{

if (myCol.Name != "NameColumn")

{

this.myDataGridView.Rows[i].Cells[myCol.ToString()].Value = Properties.Resources.BlueImage;

}

}

}

Any ideas??

Thanks.

obrienkev  Wednesday, March 05, 2008 1:46 PM

In your example, myColumnName is an object so your add routine is trying to add the string version of an object to a columns collection when it is looking for the object itself:

Try this:

Code Snippet

DataGridViewImageColumn myColumnName = new DataGridViewImageColumn();

myColumnName.Name = "col" + "1";

dataGridView1.Columns.Add(myColumnName, myColumnName.Name);

jbaird-pa  Thursday, March 06, 2008 1:27 AM

In your example, myColumnName is an object so your add routine is trying to add the string version of an object to a columns collection when it is looking for the object itself:

Try this:

Code Snippet

DataGridViewImageColumn myColumnName = new DataGridViewImageColumn();

myColumnName.Name = "col" + "1";

dataGridView1.Columns.Add(myColumnName, myColumnName.Name);

jbaird-pa  Thursday, March 06, 2008 1:27 AM

You can use google to search for other answers

Custom Search

More Threads

• network version?
• Microsoft Document Viewer
• database account configuration in Web.config file
• saving in an image format
• Including Folders into my solution
• Comma (,) as decimal point?
• A tabpage for each folder in Application.StartupPath
• reading a programs version
• paste in other programs
• soundplayer class