469,607 Members | 2,088 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

Setting Single Cell in DataGridView

My apologies upfront for what I am sure is a newbie question, but I
have been unable to find an answer via search.

I am a VB6 programmer trying to write programs in VB 2008 Express.

I have an 'unbound' datagrid control view on a form and just want to
be able to write text to individual cells.

I can programatically set the number of rows and cells, but have been
unable to select and/or write to cells.

Simple things that worked for the FlexGrid view in VB6 don't seem to
apply;

I can't just write (on a command button click event)
me.datgridview1.row= 1 ; me.datgridview1.col= 1 ;
me.datgridview1.text= "something"

These all seem to result in syntax errors.

How do I do a simple thing like this?

Thanks in advance
Jul 19 '08 #1
7 13264
A DataGrid is .NET is made up of collections of rows and columns. These
collections contain row and column objects respectively. In .NET, all
collections are zero-based.

So, if you wish to get to the second row of a DataGrid, you'd type:

dataGridName.Rows(1)

and if you wanted to get access to the third column (called items in a row
object), you'd type:

dataGridName.Rows(1).Item(2)

....and if you wanted to place a value in that column, you'd type:

dataGridName.Rows(1).Item(2) = "something"

One word of advice as you move to .NET from VB 6.0; understand the working
in VB .NET is not at all like working in VB 6.0, in terms of the objects
you'll use and the way the language is compiled. It is actually best to
discard your VB 6.0 knowledge (hard as that may be to accept) and start
fresh.

-Scott

"bobk" <in**@minuteman-systems.comwrote in message
news:01**********************************@f36g2000 hsa.googlegroups.com...
My apologies upfront for what I am sure is a newbie question, but I
have been unable to find an answer via search.

I am a VB6 programmer trying to write programs in VB 2008 Express.

I have an 'unbound' datagrid control view on a form and just want to
be able to write text to individual cells.

I can programatically set the number of rows and cells, but have been
unable to select and/or write to cells.

Simple things that worked for the FlexGrid view in VB6 don't seem to
apply;

I can't just write (on a command button click event)
me.datgridview1.row= 1 ; me.datgridview1.col= 1 ;
me.datgridview1.text= "something"

These all seem to result in syntax errors.

How do I do a simple thing like this?

Thanks in advance

Jul 19 '08 #2
On Jul 19, 4:42*pm, "Scott M." <s-...@nospam.nospamwrote:
A DataGrid is .NET is made up of collections of rows and columns. *These
collections contain row and column objects respectively. *In .NET, all
collections are zero-based.

So, if you wish to get to the second row of a DataGrid, you'd type:

dataGridName.Rows(1)

and if you wanted to get access to the third column (called items in a row
object), you'd type:

dataGridName.Rows(1).Item(2)

...and if you wanted to place a value in that column, you'd type:

dataGridName.Rows(1).Item(2) = "something"

One word of advice as you move to .NET from VB 6.0; understand the working
in VB .NET is not at all like working in VB 6.0, in terms of the objects
you'll use and the way the language is compiled. *It is actually best to
discard your VB 6.0 knowledge (hard as that may be to accept) and start
fresh.

-Scott

"bobk" <i...@minuteman-systems.comwrote in message

news:01**********************************@f36g2000 hsa.googlegroups.com...
My apologies upfront for what I am sure is a newbie question, but I
have been unable to find an answer via search.
I am a VB6 programmer trying to write programs in VB 2008 Express.
I have an 'unbound' datagrid control view on a form and just want to
be able to write text to individual cells.
I can programatically set the number of rows and cells, but have been
unable to select and/or write to cells.
Simple things that worked for the FlexGrid view in VB6 don't seem to
apply;
I can't just write (on a command button click event)
me.datgridview1.row= 1 ; me.datgridview1.col= 1 ;
me.datgridview1.text= "something"
These all seem to result in syntax errors.
How do I do a simple thing like this?
Thanks in advance- Hide quoted text -

- Show quoted text -
Thank you but that does not work.

It gives an error message that "item is not a member of
System.Windows.Forms.DataGridViewRow"

Here is the whole procedure;

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.DataGridView1.Rows(1).item(1) = "something"
End Sub

Is there some declaration or something I have to include to get it to
recognize the control/collection?

Bob
Jul 20 '08 #3
Sorry, change .Item to .Cells.
"bobk" <in**@minuteman-systems.comwrote in message
news:9e**********************************@d77g2000 hsb.googlegroups.com...
On Jul 19, 4:42 pm, "Scott M." <s-...@nospam.nospamwrote:
A DataGrid is .NET is made up of collections of rows and columns. These
collections contain row and column objects respectively. In .NET, all
collections are zero-based.

So, if you wish to get to the second row of a DataGrid, you'd type:

dataGridName.Rows(1)

and if you wanted to get access to the third column (called items in a row
object), you'd type:

dataGridName.Rows(1).Item(2)

...and if you wanted to place a value in that column, you'd type:

dataGridName.Rows(1).Item(2) = "something"

One word of advice as you move to .NET from VB 6.0; understand the working
in VB .NET is not at all like working in VB 6.0, in terms of the objects
you'll use and the way the language is compiled. It is actually best to
discard your VB 6.0 knowledge (hard as that may be to accept) and start
fresh.

-Scott

"bobk" <i...@minuteman-systems.comwrote in message

news:01**********************************@f36g2000 hsa.googlegroups.com...
My apologies upfront for what I am sure is a newbie question, but I
have been unable to find an answer via search.
I am a VB6 programmer trying to write programs in VB 2008 Express.
I have an 'unbound' datagrid control view on a form and just want to
be able to write text to individual cells.
I can programatically set the number of rows and cells, but have been
unable to select and/or write to cells.
Simple things that worked for the FlexGrid view in VB6 don't seem to
apply;
I can't just write (on a command button click event)
me.datgridview1.row= 1 ; me.datgridview1.col= 1 ;
me.datgridview1.text= "something"
These all seem to result in syntax errors.
How do I do a simple thing like this?
Thanks in advance- Hide quoted text -

- Show quoted text -
Thank you but that does not work.

It gives an error message that "item is not a member of
System.Windows.Forms.DataGridViewRow"

Here is the whole procedure;

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.DataGridView1.Rows(1).item(1) = "something"
End Sub

Is there some declaration or something I have to include to get it to
recognize the control/collection?

Bob
Jul 20 '08 #4
Scott,

I partially respectful disagree this sentence below with you (and I think
you agree with that because your text can in my idea be read in different
ways).
>
One word of advice as you move to .NET from VB 6.0; understand the working
in VB .NET is not at all like working in VB 6.0, in terms of the objects
you'll use and the way the language is compiled. It is actually best to
discard your VB 6.0 knowledge (hard as that may be to accept) and start
fresh.
The first part I agree of course completely, however the second part not.

The first part is about dotNet and OOP.

The second part is about a program languages that can be used with dotNet.
The VB6 language part is still almost completely the same inside VB9
(although that is extended) and that part can be used to create applications
using VB. However, now in a OOP way, which is (mostly) completely different
from the way VB6 developpers did it before they knew the advantages of OOP.

See this text just as a simple addition for the OP, before he becomes scared
what is absolute not necessary.

=Cor

Jul 20 '08 #5
bobk wrote:
>
"dataGridName.Rows(1).Item(2) "

"item is not a member of System.Windows.Forms.DataGridViewRow"

I'm sure there's something else I'm doing wrong and would greatly
appreciate any help figuring out what it is.
Not using Intellisense? :)

You have a DataGridView, not a DataGrid. So you need to use its properties:

DataGridView1.Item(2, 1).Value = "Boo"
or
DataGridView1.Rows(1).Cells(2).Value = "Boo"

Jul 20 '08 #6
On Jul 20, 3:36*pm, "Steve Gerrard" <mynameh...@comcast.netwrote:
bobk wrote:
"dataGridName.Rows(1).Item(2) "
"item is not a member of *System.Windows.Forms.DataGridViewRow"
I'm sure there's something else I'm doing wrong and would greatly
appreciate any help figuring out what it is.

Not using Intellisense? :)

You have a DataGridView, not a DataGrid. So you need to use its properties:

* * DataGridView1.Item(2, 1).Value = "Boo"
or
* * DataGridView1.Rows(1).Cells(2).Value = "Boo"
That does it.

Thank You.
Jul 20 '08 #7
>COMMENT: At each step the list of choices is sometimes long and often
>not obvious, at least to someone coming from VB6 to .NET. I have a
text book I'm working with and I tried scouring the help files and
searching the newsgroups, but could not find, until here, a clear
explanation of the concepts you are describing. Again I appreciate
that.
This is why I say you should back up and learn more about object-oriented
programming before diving into VB .NET (or any .NET language).

When you see that dropdown list (called IntelliSense) the icons next to the
items you see indicate what kind of "member" you are looking at; the
pointing hand icon depicts a "property" and the pink block depicts a
"method". Knowing that properties describe something and that methods are
actions (functions) that can be performed usually allows you to immediately
rule out one type on the list. When that leaves you with a subset of
choices, you can simply select the one that seems like it may be what you
are looking for and as soon as it is in your code, simply press F1, which
will immediately take you to the help for that choice. You can then read
about that choice and see if it is what you are looking for.

At last count, there are over 50,000 types in the .NET Framework!!! Each one
has its own properties and methods and no one knows what they all do. The
best path is to learn how to use Visual Studio to learn the .NET Framework.

Good luck.


Jul 21 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Veeneman | last post: by
2 posts views Thread by galletg | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.