Results 1 to 2 of 2

Thread: Populating variables from DataGridView selections

  1. #1
    Nucleus is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    May 2005
    Posts
    30
    Reputation
    147

    Populating variables from DataGridView selections

    vb.net has a steep learning curve, so please bare with me.

    I am trying to create a program where you can select multiple Linux hosts, then select a bash script (which already exists on the Linux hosts), and click a button to execute the script on all the hosts. I've managed to get vb.net to read from XML files, and list all the available commands and all the available hosts. I've also found a way to connect on a host using Renci.SshNet.

    What I need to do now, is configure a button, to see which hosts I have selected, and which bash script I have selected, and run the script on those hosts.

    This is the GUI.
    gui.png

    And this is the code
    Code:
    Public Class SSHCommander
        Dim cmd As Renci.SshNet.SshCommand
        Dim hostsPath As String = "hosts.xml"
        Dim commandsPath As String = "commands.xml"
        Dim connInfo As New Renci.SshNet.PasswordConnectionInfo("hostname", "22", "user", "pass")
        Private Sub SSHCommander_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CommandsDataSet.ReadXml(commandsPath)
            CommandsGridView.DataSource = CommandsDataSet
            CommandsGridView.DataMember = "commands"
            HostsDataSet.ReadXml(hostsPath)
            HostsGridView.DataSource = HostsDataSet
            HostsGridView.DataMember = "hosts"
        End Sub
        Private Sub execute_command_Click(sender As Object, e As EventArgs) Handles execute_command.Click
            Dim sshClient As New Renci.SshNet.SshClient(connInfo)
            Using sshClient
                sshClient.Connect()
                cmd = sshClient.RunCommand("selected command must be here")
                rtbresult.Text = cmd.Result
                sshClient.Disconnect()
            End Using
        End Sub
    End Class
    Basically, the "connInfo" variable fields must be populated by the "Hosts" DataGridView selection on the GUI, and the "sshClient.RunCommand" fields must be populated by the "Commands" DataGridView selection on the GUI. Another problem I must solve, is how do I handle multiple hosts selections?

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,085
    Reputation
    2803
    A little help:
    For Each row As DataGridViewRow In CommandsDataGridView.SelectedRows
    Dim data = CType(row.DataBoundItem, DataRowView)
    Dim value = data("Hostname")

    Next
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

Bookmarks

Posting Permissions

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