Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,156 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,113 people online right now. Registration is fast and FREE... Join Now!




Cannot add cells to rows in DataGridView

 
Reply to this topicStart new topic

Cannot add cells to rows in DataGridView

cygnusX
28 Dec, 2007 - 08:31 AM
Post #1

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
CODE

DataGridViewRow row = new DataGridViewRow();
DataGridViewCell c1 = row.Cells["values"];
DataGridViewCell c2 = row.Cells["user"];


What's wrong with that code?I'm getting exception saying that i do not have column named 'values',but i have such column.If i set breakpoint before DataGridViewCell c1 = row.Cells["values"]; i can see that there are two columns in the datagridview named 'values' and 'user'.Even if i use DataGridViewCell c1 = row.Cells[0]; i'm getting another exception saying this "Index was out of range. Must be non-negative and less than the size of the collection."What's wrong?
User is offlineProfile CardPM
+Quote Post

phatSolutions
RE: Cannot Add Cells To Rows In DataGridView
28 Dec, 2007 - 10:22 AM
Post #2

New D.I.C Head
*

Joined: 21 Dec, 2007
Posts: 12

QUOTE(cygnusX @ 28 Dec, 2007 - 09:31 AM) *

CODE

DataGridViewRow row = new DataGridViewRow();
DataGridViewCell c1 = row.Cells["values"];
DataGridViewCell c2 = row.Cells["user"];


What's wrong with that code?I'm getting exception saying that i do not have column named 'values',but i have such column.If i set breakpoint before DataGridViewCell c1 = row.Cells["values"]; i can see that there are two columns in the datagridview named 'values' and 'user'.Even if i use DataGridViewCell c1 = row.Cells[0]; i'm getting another exception saying this "Index was out of range. Must be non-negative and less than the size of the collection."What's wrong?



Hmm, im going to have to do some testing but you could try casting the row.Cells to a DataGridViewCell like so:

CODE
DataGridViewCell c1 = (DataGridViewCell)row.Cells["values"];


havent tested it yet, but I will and see what I come up with.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Cannot Add Cells To Rows In DataGridView
28 Dec, 2007 - 11:11 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Well, for this:
CODE

DataGridViewRow row = new DataGridViewRow();
DataGridViewCell c1 = row.Cells["values"];


The row is new and is completely empty. Presumably you're getting the row from an active dataGrid?

If so, write a little code to dump out what's really there, e.g.
CODE

System.Diagnostics.Debug.WriteLine("ColumnCount = " + row.DataGridView.ColumnCount);
foreach(DataGridViewColumn col in row.DataGridView.Columns) {
   System.Diagnostics.Debug.WriteLine(col.Index + " : " + col.Name);
}


Or just put a debugger break at the row call and walk around.

Hope this helps.



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:21PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month