Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Click event on Datagrid

MokoneMokone Posts: 20Member
Hi all,
I have a datagrid that display the result of my query (EmployeeId and EmployeeName) and now I want to click on a datagrid row to get the EmployeeId to the next form.

Your help will be appreciated.

Comments

  • jaakinyejaakinye Posts: 12Member
    Please be specific
    What order [color=Red][size=3]form[/size][/color] do you mean?
  • MokoneMokone Posts: 20Member
    Hi,
    I have two forms: frmMain and frmDisplay. I have datagrid on the frmMain that display the results of a query. Now what I want is to click on one of the rows of datagrid and call frmDiplay with the EmployeeId of the row clicked on frmMain. what I basically need is to click datagrid on frmMain and call frmDisplay to display the EmployeeId.
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    I am not sure if you want to have a new instance of frmDisplay for each time you click on that datagrid or if you want the same to change. I am going to assume you want the same to change so here is one method: (there are other equally correct solutions)

    On your main form, declare a global variable:
    [code] Dim fDisp as frmDisplay [/code]

    On your frmDisplay, you need a Public Sub or Public Function that you can pass the value you want to display. I don't know anything about your program, so here is an example of what it would look like:
    [code]
    Public Sub displayValue(ByVal Str as String)
    Me.TextBox1.Text = Str
    End Sub
    [/code]

    On you main form's datagrid's click event, you have to determine if frmDisplay is open, and pass the value to it in either case... Here is an example of what that would look like. You might have a question about a command I am using, I explain it below
    [code]
    If Not isNothing(fDisp) AndAlso Not isDisposed(fDisp)
    'fDisp is currently open
    If DataGrid1.SelectedIndicies(0) >= 0 Then
    fDisp.displayValue(DataGrid1.Item(DataGrid1.SelectedIndicies(0)))
    fDisp.Show()
    End If
    Else
    'fDisp has either been closed, or never openned
    If DataGrid1.SelectedIndicies(0) >= 0 Then
    fDisp = new frmDisplay
    fDisp.Show()
    fDisp.displayValue(DataGrid1.Item(DataGrid1.SelectedIndicies(0)))
    End If
    End If
    [/code]

    AndAlso and OrElse are called Circut Logic. When you use And and Or in If statements, it evaluates ALL of the cases AT ONCE. When you use AndAlso and OrElse, it checks each case individually and stops evaluating as soon as a condition is fulfilled. This way, you can avoid Null reference errors, and use only one if statement instead of embedded if's.

    Search for more info on AndAlso and OrElse if you still don't understand them. Hope I helped!
  • MokoneMokone Posts: 20Member
    Hi,
    Thank you so much. I don't know what I would without you guys. You guys are really helping and inspiring us "the bigginers programmers".

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    No problem, we're here to help if we can. Kinda wish there was a tad bigger of a community solving VB.Net issues, but this works.

    -Sean C
Sign In or Register to comment.