Results 1 to 7 of 7

Thread: DataGridView - Add Text To Cell

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    DataGridView - Add Text To Cell

    Hi,

    I want to add text to a specific row, column in a DataGridView grid. I have tried several ways to do this bit none seem to work.

    Is there a way to do this?

    Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: DataGridView - Add Text To Cell

    Use:

    Code:
    datagridview.rows(rownumber).cells(columnnumber).value = text string
    
    or
    
    datagridview.rows(rownumber).cells("column name").value = text string
    Computerman
    It was much easier in VB6, but I am now liking Vb.Net alot more.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataGridView - Add Text To Cell

    Quote Originally Posted by computerman View Post
    Use:

    Code:
    datagridview.rows(rownumber).cells(columnnumber).value = text string
    
    or
    
    datagridview.rows(rownumber).cells("column name").value = text string
    Computerman
    It's a bit more succinct to index the grid directly:
    vb.net Code:
    1. myGrid(columnIndex, rowIndex).Value = someValue
    I'd only go via the row if I already had a reference to the row.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    Re: DataGridView - Add Text To Cell

    Hi,
    Thanks for your replies.
    I tried the following but both ways don't seem to work:-

    frmSearch.grdDB.Rows(2).Cells(1).Value = "Test"
    frmSearch.grdDB(2, 1).Value = "Test"

    I've attached a screenshot showing the error I get.

    Thanks.
    Attached Images Attached Images  

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataGridView - Add Text To Cell

    That's like someone teaching you how to drive and then you saying that it doesn't work if you drive into a wall. Both ways DO work and they are both working, otherwise you wouldn't be getting that error message. In order to get that error message the cell value has to be being set. The problem is that you're setting it to an invalid value. You can't assign the string "Test" to a cell that expects a number, which is what the error message is telling you.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    4

    Re: DataGridView - Add Text To Cell

    G'day jmcilhinney,

    You're right! I've just tried it again and it works.

    I'm converting from VB5 to VB.net so it does feel like driving into a wall a times.

    Thanks for you response.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataGridView - Add Text To Cell

    Fasten your seat belt and keep your hands inside the vehicle at all times!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width