Results 1 to 7 of 7

Thread: Cannot compile a line

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,248

    Cannot compile a line

    I'm entering this line in DataGridView1_CellMouseClick

    kCell = dataGridView1[col,21].Value;

    Col is an Integer and set = (e.ColumnIndex)
    The pop up error thing suggests it needs a closing ] after col, but that doesn't make any sense, nor does it work.

    Should the line work Ok ? It's very hard to know as my Visual Studio 2010 Express may be corrupted somehow because I can't print LEN or CHR
    in the Immediate window without getting an error (and others can).

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: Cannot compile a line

    Should the line work Ok ?
    No, the syntax is not right. Try this...
    Code:
     kCell = DataGridView1.Rows(21).Cells(col)
    I can't print LEN or CHR in the Immediate window without getting an error (and others can).
    What errors do you get when you try? Those functions are part of vb namespace and unless you have imported it, they will not be available.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,623

    Re: Cannot compile a line

    Your syntax is wrong,

    you need to access a particular cell through the Rows collection e.g ;

    Code:
    dataGridView1.Rows[0].Cells[1].Value
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,248

    Re: Cannot compile a line

    Thanks for that, example came off the net somewhere. It now compiles conditionally but for some reason I can't
    do anything with what's returned.
    Code:
    kCell = DataGridView1.Rows(e.RowIndex).Cells(21)
    This is with kCell dimmed as an Object, if Dimmed as a string it errors reporting can't convert it to a string.
    I've also tried Cstr() and debug.print kCell , but nothing works.

    I'm using concepts from older forms or Basic, so I expect a few hassles like this.... but how would you get
    the value in a DataGridView cell as a string ?

    Thanks

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Cannot compile a line

    You are doing yourself a huge disservice by copying and pasting code fragments that you find on the internet (in this case in the incorrect language, C#). The VB IDE has several features built into it that, if paid attention to, will teach you a lot. The most useful feature is known as Intellisense which will try to guess what you want as you type it in and present help along the way.

    This image shows three different ways to reference a DataGridViewCell and the Intellisense help presented.
    Name:  DGV Cell.jpg
Views: 36
Size:  22.4 KB

    Now you what to retrieve the Value property on the referenced DataGridViewCell. If you take the time to look at the documentation for this property, you will see that it returns an Object type. You want to cast this Object to a string.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,248

    Re: Cannot compile a line

    Thanks for responding. I did read though the GetValue (which seems what I wanted) section in your link, but to be honest i didn't get much from it.
    I find asking and trying things out teaches me better. Those articles can be very jargon-heavy and by the time you're worked out what they mean, you've forgotten what you wanted!
    (They seldom explain something in a way I can understand, but I do have a go.)

    Ok, it returns an Object type. Looking for ways to cast an object to a string, I found this to try - Convert.ToString(Var)

    So I attempted this
    Code:
    kCell = DataGridView1.Rows(e.RowIndex).Cells(21)
    Debug.Print(Convert.ToString(kCell))
    (BTW I read someone Debug.print is no longer valid and you use Debug.Write but that's wrong too)

    You probably know what this did, wasn't what was wanted, but maybe inching closer. At least it did something !

    I see mention of many ways to 'cast' including Cstr which was the first one I tried. Something will work eventually

    Regards, ABB

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,248

    Re: Cannot compile a line

    Quote Originally Posted by kebo View Post
    What errors do you get when you try? Those functions are part of vb namespace and unless you have imported it, they will not be available.
    kevin
    Thanks Kebo, debug.print chr(34) returns
    'chr' is not declared. It may be inaccessible due to its protection level.

    But ? Microsoft.VisualBasic.Chr(34) works

    I read through How to: Add or Remove Imported Namespaces (Visual Basic) and found Microsoft.VisualBasic is present and ticked.

    Is that the namespace required, or is something else needed ?

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