Results 1 to 19 of 19

Thread: Datagrid Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129

    Question Datagrid Question

    Hi All,

    I have created a form with a datagrid. What i would like to do is double click on a row within the datagrid to open another form displaying the contents of that record.

    I already have the other form to display the selected record but I don't know how to get the value of cell containing the primary key so i can open the form and jump to that record.

    any help or examples appreciated.

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    I'm in the middle of pulling some data, so you have to give me a while so I can actually look and see what exactly it is, but the datagrid has a double click method, and it also has a CurrentCell property with the row and column number in it...so:

    get the cell data by:

    CellData = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, DataGrid1.CurrentCell.ColumnNumber)
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    Many thanks for your reply, it got me on the right track and I am now starting to make some progress.

  4. #4
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Unhappy please help a newbie!!!

    hi!

    Just a few questions...what is the datatype of the variable CellData? Where will i put this code? in the datagrid.doubleclick event or in the new form that will pop up? please help a newbie! Thanks!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    celldata is a public variable stored within a module.

    VB Code:
    1. Public celldata as Int

    This is then available for use in any of the forms. Remember to re-set it everytime you load your form.

    VB Code:
    1. celldata = ''

  6. #6
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    thanks for replying! Since you've been nice enough to share your knowledge with me, would you mind sharing the code you used in your program? It's ok if you don't want to. I understand. But I'd be very grateful if you do. Thanks! Thanks!

    PS:
    1. Was your second form in datagrid also or you used textboxes/comboboxes?
    2. Was your datagrid bound to a dataset or a datatable?
    Last edited by siomai; Aug 23rd, 2004 at 04:28 AM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    Hello again,

    i will explain how i achieved it. I had two forms, the first contained a datagrid containing the records. Once i double clicked a record, the second form then opened showing all the details for the record i had chosen.

    VB Code:
    1. Private Sub grdData_Doubleclick(ByVal sender As Object, ByVal e AS System.EventArgs) Handles grdData.DoubleClick
    2.  
    3. 'grab the row
    4. CellData = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, DataGrid1.CurrentCell.ColumnNumber)
    5.  
    6. If CellData = '' Then
    7. msgBox("No Record")
    8. Else
    9. 'make call to public function in Module
    10. Call Open_Form1
    11. End If
    12.  
    13. End Sub

    Now you need to add a module to your project and make sure you have the function Open_Form1 (or whatever you call it)

    VB Code:
    1. 'you need to declare you celldata variable here
    2. Public celldata as Int
    3.  
    4.  
    5.     Public Function Open_Form1 ()
    6.  
    7.         'open a new instance of the form
    8.         Dim frm1 As New frm1
    9.         frm1.Show()
    10.         'on my form I have a combobox so i use this to pass the value of celldata to.
    11.         frm1.cboData.SelectedValue = celldata
    12.  
    13.     End Function

    It's a bit rough and ready but it works.

    Hope this helps
    Last edited by dagoose; Aug 23rd, 2004 at 05:27 AM.

  8. #8
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    hi again!

    I'm having a blue line in this line:

    VB Code:
    1. If celldata = ' '  Then

    It says "Expression Expected."

    Is this in vb.net code? Cause single quote is for comments. Think it's commenting the succeeding code out. Btw, I declared variable celldata as integer. Was that right?
    Last edited by siomai; Aug 23rd, 2004 at 10:12 PM.

  9. #9
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    Ok...instead of putting the ' ' in the expression, I used 0. It didn't give me any errors but did not even open my form. what happend?

    Also, after (for example) I have already editted the data in my second form, how will I overwrite the old celldata with the new and editted data? Please help!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    oops! should have been "" not ''

    did you put the function inside the module?

  11. #11
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Unhappy got an error

    hi dagoose!

    I changed my code and placed "not" in the expression...i got this error:

    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "not" to type 'Double' is not valid.
    Hope you could help me...i need this very badly. Thanks in advance!

  12. #12
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    hope you'd reply soon...

    btw, what is 'Int' in your code?

    Public celldata as Int

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    int = Integer

    You mis-understood my post, don't use the word not, use "" (empty quotes).

  14. #14
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    ok i used revised my code but still i got this error:

    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "" to type 'Double' is not valid.

    why? what am i doing wrong?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    could be that you field in the database is a different datatype, try changing to:

    Public celldata as String

  16. #16
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Thumbs up double click on datagrid (RESOLVED)

    dagoose!!!!hooray! i got it! I want to thank you cause you've been very nice and helpful to me.

    Now, i'm facing a problem on how to put the data on my textboxes and comboboxes from my second form (the one that popped up when my datagrid was double clicked) back to the row that i clicked on in my datagrid.

    Could you help me on this? what did you do with your program if you don't mind me asking?

    Super thanks dagoose! You've been a great help to me! Take care now.

  17. #17
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    help! please! I can't do it...
    i'm getting dizzy already....please help me! anyone?

  18. #18
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Unhappy double click on datagrid (CONTINUATION)

    please help me do this...

    I'm able to doubleclick on my datagrid and transfer the data from the datagrid row to my 2nd form. What I want to do now is to have the edited data from my 2nd form to my main form (in the datagrid). How will I do this? I've been trying to solve this by myself but I just can't! Please somebody help me!

  19. #19
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    anyone? please...i've been working on this for a couple of days now...but still to no avail! hope someone could show me how to do this.

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