Setting Single Cell in DataGridView

  • Thread starter Thread starter bobk
  • Start date Start date
B

bobk

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
 
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
 
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














- 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
 
Sorry, change .Item to .Cells.


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














- 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
 
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
 
As someone who has gone through this and has taught countless others VB
..NET, I stand by my statment. But, I may need to explain it a bit further.

When you come to VB .NET from VB and you make assumptions about what
"should" work, you will most likely not be taking the correct route. So my
advice is forget what you know and come into VB .NET fresh and willing to
learn from scratch.

As you then learn (or relearn) VB .NET, you will find certain language
elements that haven't changes (decision making code and loops come to mind).
And, you'll then feel good about your prior knowledge in that particular
area.

But to assume that all the language elements work as they used to is
incorrect. For example, declaraing an Integer is still done the same way in
VB .NET, but now, you'll get a 32 bit integer, rather than the 16 bit one
you got in VB 6. Now, if you declare a variable (exactly the same way you
used to in VB 6) inside a coding block (If...Then, etc.), you'll get block
level scope, which didn't exist before.

No, I said what I meant. In order to not get burned, it's best to start
from scratch and not assume anything. If you encounter something along the
way that still is the way you learned it before, that's just a bonus.

-Scott
 
As someone who has gone through this and has taught countless others VB
.NET, I stand by my statment.  But, I may need to explain it a bit further.

When you come to VB .NET from VB and you make assumptions about what
"should" work, you will most likely not be taking the correct route.  So my
advice is forget what you know and come into VB .NET fresh and willing to
learn from scratch.

As you then learn (or relearn) VB .NET, you will find certain language
elements that haven't changes (decision making code and loops come to mind).
And, you'll then feel good about your prior knowledge in that particular
area.

But to assume that all the language elements work as they used to is
incorrect.  For example, declaraing an Integer is still done the same way in
VB .NET, but now, you'll get a 32 bit integer, rather than the 16 bit one
you got in VB 6.  Now, if you declare a variable (exactly the same way you
used to in VB 6) inside a coding block (If...Then, etc.), you'll get block
level scope, which didn't exist before.

No, I said what I meant.  In order to not get burned, it's best to start
from scratch and not assume anything.  If you encounter something alongthe
way that still is the way you learned it before, that's just a bonus.

-Scott












- Show quoted text -

I would just like to know how to make VB 2008 Express work. As I said
in an earlier post, the suggested way of accessing one cell via

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

does not work; it resultrs in an error message;

"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.

Thanks,

Bob
 
I already responded to that and told you that I mistakenly told you to use
Item, when it should be Cells.

But, this really brings up a larger point. When you type:

dataGridName.Rows(1) and then the dot (.), you will get a list of possible
choices that could be added at that point. If you don't understand that
what you *can* add at that point must be on the list you see, then you
should back way up and start learning about object-oriented programming from
the beginning.

Please don't think I'm trying to be rude, because I'm not, but there are so
many times when even a seasoned VB .NET developer is going to come up
against an object they've never worked with before. But understanding the
basics allows us to deduce what we need by letting Visual Studio help us.
In other words, the message you got about Item not being a member of Rows
should tell you loud and clear that you can't use Item with Rows, but on the
other hand what was on the drop-down list after you typed the period? A
quick examination of that list should reveal some good possiblie choices.
If you are still unclear what to choose, you could very easily go into the
help on the Rows keyword and see what it says there.

-Scott

As someone who has gone through this and has taught countless others VB
.NET, I stand by my statment. But, I may need to explain it a bit further.

When you come to VB .NET from VB and you make assumptions about what
"should" work, you will most likely not be taking the correct route. So my
advice is forget what you know and come into VB .NET fresh and willing to
learn from scratch.

As you then learn (or relearn) VB .NET, you will find certain language
elements that haven't changes (decision making code and loops come to
mind).
And, you'll then feel good about your prior knowledge in that particular
area.

But to assume that all the language elements work as they used to is
incorrect. For example, declaraing an Integer is still done the same way
in
VB .NET, but now, you'll get a 32 bit integer, rather than the 16 bit one
you got in VB 6. Now, if you declare a variable (exactly the same way you
used to in VB 6) inside a coding block (If...Then, etc.), you'll get block
level scope, which didn't exist before.

No, I said what I meant. In order to not get burned, it's best to start
from scratch and not assume anything. If you encounter something along the
way that still is the way you learned it before, that's just a bonus.

-Scott












- Show quoted text -

I would just like to know how to make VB 2008 Express work. As I said
in an earlier post, the suggested way of accessing one cell via

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

does not work; it resultrs in an error message;

"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.

Thanks,

Bob
 
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.
 
I already responded to that and told you that I mistakenly told you to use
Item, when it should be Cells.

But, this really brings up a larger point.  When you type:

dataGridName.Rows(1) and then the dot (.), you will get a list of possible
choices that could be added at that point.  If you don't understand that
what you *can* add at that point must be on the list you see, then you
should back way up and start learning about object-oriented programming from
the beginning.

Please don't think I'm trying to be rude, because I'm not, but there are so
many times when even a seasoned VB .NET developer is going to come up
against an object they've never worked with before.  But understanding the
basics allows us to deduce what we need by letting Visual Studio help us.
In other words, the message you got about Item not being a member of Rows
should tell you loud and clear that you can't use Item with Rows, but on the
other hand what was on the drop-down list after you typed the period?  A
quick examination of that list should reveal some good possiblie choices.
If you are still unclear what to choose, you could very easily go into the
help on the Rows keyword and see what it says there.

-Scott









I would just like to know how to make VB 2008 Express work. As I said
in an earlier post, the suggested way of accessing one cell via

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

does not work; it resultrs in an error message;

"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.

Thanks,

Bob- Hide quoted text -

- Show quoted text -

RE:"I already responded to that and told you that I mistakenly told
you to use
Item, when it should be Cells.

COMMENT: My apologies. I missed that. It was brief and I'm still
getting used to using Google to read newsgroups. My service provider
dropped news services and I can't use my old and familiar newsreader
anymore. Again, your assistance ois greatly appreciated.

RE: "dataGridName.Rows(1) and then the dot (.), you will get a list of
possible
choices that could be added at that point. If you don't understand
that
what you *can* add at that point must be on the list you see, then
you
should back way up and start learning about object-oriented
programming from
the beginning.

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.
 
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.
 
Back
Top