Results 1 to 12 of 12

Thread: Datagrid

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Datagrid

    Hi

    I have command button & i want when clicked another form should open in which Data is displayed & when user double clicks on Datagrid row it should return values to parent form.

    Thanks

  2. #2
    gibra
    Guest

    Re: Datagrid

    Sorry, but you are not clear (to me at least) ...

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,439

    Re: Datagrid

    Quote Originally Posted by gibra View Post
    Sorry, but you are not clear (to me at least) ...
    He doesn't know, how to make the communication between parent-form and child-form
    Parent-Form has a commandbutton, and probably some labels or textboxes.
    He hits the command-button, another form opens up, which has a datagrid (whichever).
    He doubleclicks a row in that datagrid, and ... presto: the selected data from the datagrid is in the lables/textboxes on the parent form
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Datagrid

    When you double-click on your datagrid, you can opt to assign values from that grid to global variables (set in a module), or you can assign those values directly to controls on your main form, like:

    Dim myGridValue as String
    myGridValue = (whatever cell(s) you want, assign it(them) here)
    myMainForm.Text1.Text = myGridValue

    or more simply:
    myMainForm.Text1.Text = (whatever cell(s) you want, assign it(them) here)

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Datagrid

    If I might offer a slightly different approach. I'd create a property or properties (or functions, or something) on the child form, and have the calling form get the values that way. I wouldn't have the child form force the data into the parent. While Sam's solution is simple and should work... it has a high risk of breaking down the road. It also relies heavily on default instances of forms... which in VB6 probably isn't a huge deal, but could still be a problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Datagrid

    Sam's solutions are usually simple, because that is all I understand! :-) I leave the complicated stuff to the experts!

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Datagrid

    I have never saw any issues from using default form instances in VB6.

    Another option would be to use a public recordset or pass an instance of the recordset between forms.

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Datagrid

    It's never a problem as long as that's all you use...

    but if the form is a created instance, then using this:
    myMainForm.Text1.Text = myGridValue

    will cause the data to be sent to the default instance rather than the specific instance. That's all. I'm used to working with multiple instances of forms, as well as child forms that are used in more than one place, so I've always used the properties method.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Datagrid

    I use default instances and created instances as needed. Some projects have a mixture of the two. This does not cause any problems unless of course I have some errors in my code but if that is the case then I can't blame the form instances for that.

  10. #10
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Datagrid

    Quote Originally Posted by Zvoni View Post
    He doesn't know, how to make the communication between parent-form and child-form
    Parent-Form has a commandbutton, and probably some labels or textboxes.
    He hits the command-button, another form opens up, which has a datagrid (whichever).
    He doubleclicks a row in that datagrid, and ... presto: the selected data from the datagrid is in the lables/textboxes on the parent form
    Well, in this case using an ADO-Recordset is the simplest solution.
    (DataMiser already hinted at that).

    This ADO-Rs could be hosted (declared) in a cModel-Class - or in the
    ParentForm directly (as a Private Variable).

    In the following short Demo-Project-Setup, I choose hosting of the Rs in the ParentForm.

    Requirements to run the example:
    - ensure a new, empty StdExe-Project
    - ensure two normal Forms in it (Form1 as our Parent-Form, Form2 will be our Grid-Form)
    - add an ADO 2.5 reference to the Project (over the References Dialogue)
    - add a "Microsoft DatGrid" to the Project (over the Components Dialogue)
    - place a DataGrid-instance on Form2 (with its defaultname DataGrid1)

    The above preparations will take only one minute...

    Now place the following code within our parent-form (Form1):

    Code:
    Option Explicit
    
    Private Rs As New ADODB.Recordset
    Private WithEvents cmdShowGridForm As CommandButton
    Private txtID As TextBox, txtName As TextBox, cmbGender As ComboBox
    
    Private Sub Form_Load()
      Rs.Fields.Append "ID", vbLong
      Rs.Fields.Append "Name", vbString
      Rs.Fields.Append "Gender", vbString
      Rs.Open
      Rs.AddNew Array(0, 1, 2), Array(1, "Peter", "male")
      Rs.AddNew Array(0, 1, 2), Array(2, "Paul", "male")
      Rs.AddNew Array(0, 1, 2), Array(3, "Mary", "female")
      
      Set cmdShowGridForm = Controls.Add("VB.CommandButton", "cmdShowGridForm")
          cmdShowGridForm.Visible = True
          cmdShowGridForm.Caption = "Show Grid"
      
      ScaleMode = vbPixels
      Set txtID = AddAndBind("txtID", "VB.TextBox", "ID", 10, 40, 150, 23)
      Set txtName = AddAndBind("txtName", "VB.TextBox", "Name", 10, 70, 150, 23)
      Set cmbGender = AddAndBind("cmbGender", "VB.ComboBox", "Gender", 10, 100, 150)
          cmbGender.AddItem "male"
          cmbGender.AddItem "female"
    End Sub
    
    Private Function AddAndBind(Key, ProgID, DataField, x, y, dx, Optional dy)
      Set AddAndBind = Controls.Add(ProgID, Key)
          AddAndBind.DataField = DataField
      Set AddAndBind.DataSource = Rs
          AddAndBind.Visible = True
      If IsMissing(dy) Then AddAndBind.Move x, y, dx Else AddAndBind.Move x, y, dx, dy
    End Function
    
    Private Sub cmdShowGridForm_Click()
      Form2.ShowAndBind Rs, Me
    End Sub
    And the following code in the grid-form (Form2)
    Code:
    Option Explicit
    
    Public Sub ShowAndBind(Rs As ADODB.Recordset, ParentForm)
      Set DataGrid1.DataSource = Rs
      Show , ParentForm
    End Sub
    I think, that shows what's possible per ADO-DataBinding quite nicely.

    Olaf

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Datagrid

    DataGrid controls are more for entry/editing of data than displaying/picking data.

    I'd choose a flexgrid or ListView or something else designed for the described usage.

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: Datagrid

    Quote Originally Posted by dilettante View Post
    DataGrid controls are more for entry/editing of data than displaying/picking data.

    I'd choose a flexgrid or ListView or something else designed for the described usage.
    Well, the Datagrid-Control is more lightweight than e.g. a FlexGrid (since it avoids
    any DataCopying from the underlying, bound ADO-Recordset).

    And to switch off its (per default enabled) CellEditing-behaviour and make it
    show "Full-Row-Selects" instead, it is enough to enhance my small Routine -
    ShowAndBind (in Form2) about two Lines:

    Code:
       DataGrid1.AllowUpdate = False
       DataGrid1.MarqueeStyle = dbgHighlightRow
    Olaf

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