Setting Single Cell in DataGridView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bobk

    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
  • Scott M.

    #2
    Re: Setting Single Cell in DataGridView

    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.Ro ws(1)

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

    dataGridName.Ro ws(1).Item(2)

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

    dataGridName.Ro ws(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" <info@minutem an-systems.comwrot e in message
    news:01d048d9-ba86-481e-a805-6109ffe3ed05@f3 6g2000hsa.googl egroups.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

    Comment

    • bobk

      #3
      Re: Setting Single Cell in DataGridView

      On Jul 19, 4:42 pm, "Scott M." <s-...@nospam.nosp amwrote:
      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.Ro ws(1)
      >
      and if you wanted to get access to the third column (called items in a row
      object), you'd type:
      >
      dataGridName.Ro ws(1).Item(2)
      >
      ...and if you wanted to place a value in that column, you'd type:
      >
      dataGridName.Ro ws(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...@minutem an-systems.comwrot e in message
      >
      news:01d048d9-ba86-481e-a805-6109ffe3ed05@f3 6g2000hsa.googl egroups.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.DataGridV iewRow"

      Here is the whole procedure;

      Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button2.Click
      Me.DataGridView 1.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

      Comment

      • Scott M.

        #4
        Re: Setting Single Cell in DataGridView

        Sorry, change .Item to .Cells.


        "bobk" <info@minutem an-systems.comwrot e in message
        news:9eff3bdc-56f4-4eeb-918f-a47edc0453fd@d7 7g2000hsb.googl egroups.com...
        On Jul 19, 4:42 pm, "Scott M." <s-...@nospam.nosp amwrote:
        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.Ro ws(1)
        >
        and if you wanted to get access to the third column (called items in a row
        object), you'd type:
        >
        dataGridName.Ro ws(1).Item(2)
        >
        ...and if you wanted to place a value in that column, you'd type:
        >
        dataGridName.Ro ws(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...@minutem an-systems.comwrot e in message
        >
        news:01d048d9-ba86-481e-a805-6109ffe3ed05@f3 6g2000hsa.googl egroups.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.DataGridV iewRow"

        Here is the whole procedure;

        Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles Button2.Click
        Me.DataGridView 1.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


        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Setting Single Cell in DataGridView

          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

          Comment

          • Steve Gerrard

            #6
            Re: Setting Single Cell in DataGridView

            bobk wrote:
            >
            "dataGridName.R ows(1).Item(2) "
            >
            "item is not a member of System.Windows. Forms.DataGridV iewRow"
            >
            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.I tem(2, 1).Value = "Boo"
            or
            DataGridView1.R ows(1).Cells(2) .Value = "Boo"



            Comment

            • bobk

              #7
              Re: Setting Single Cell in DataGridView

              On Jul 20, 3:36 pm, "Steve Gerrard" <mynameh...@com cast.netwrote:
              bobk wrote:
              >
              "dataGridName.R ows(1).Item(2) "
              >
              "item is not a member of  System.Windows. Forms.DataGridV iewRow"
              >
              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.I tem(2, 1).Value = "Boo"
              or
                  DataGridView1.R ows(1).Cells(2) .Value = "Boo"
              That does it.

              Thank You.

              Comment

              • Scott M.

                #8
                Re: Setting Single Cell in DataGridView

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






                Comment

                Working...