none
MultiSelect from DatagridView to function question RRS feed

  • Question

  • Good Morning, 

          I am working on a small project, and I am basically a newb to this platform, but I have made a program that stores a bunch of computer names and descriptions in a sql table.  and lets a specific user reboot the pc when it stops behaving properly. I grab some data like this on specfic button clicks based on the type of computer it is.

    LoadGrid("select ServerName, Description from CaseTrackingBoards where Application = 'CareView';")

    and right now I select one row and do this 

    Private Sub btnRebootCTB_Click(sender As Object, e As EventArgs) Handles btnRebootCTB.Click
            RebootMachine(DGViewFirstNet.CurrentCell.FormattedValue)
        End Sub

    which does

        Private Sub RebootMachine(RebootServer As String)
            'MsgBox("You have chosen to reboot " & RebootServer)
            If MessageBox.Show("Are you sure you wish to reboot " & RebootServer & "?", "Confirm reboot",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then
                Using RebootProc As New Process
                    With RebootProc.StartInfo
                        .CreateNoWindow = False
                        .FileName = "shutdown.exe"
                        .Arguments = String.Format("-r -m \\{0} -c ""PC unresponsive Jake rebooted"" -t 1", RebootServer)
                        .UseShellExecute = False
                        .WindowStyle = ProcessWindowStyle.Hidden
                        .RedirectStandardOutput = True
                        .RedirectStandardInput = True
                    End With
                    RebootProc.Start()
                End Using
            End If

        End Sub

    This all works fine .. but we want a way to just reboot all 5 or 6 with one button click.  I am assuming I would have to work with the dgview.selectedrows, and I would think I would have to pop all the "ServerName"  values into an array or something and feed that into a new sub and loop through those ServerNames and pretty much do as above.  But I can't find the syntax to do this properly for VB.  This doesn't feel like it should be hard..  but I am at a loss and I am failing BING today.. :)

    Thank you for any assistance!

    Jay

    Friday, September 20, 2019 2:41 PM

Answers

  • I would recommend using a DataGridViewCheckBoxColumn

    The extension gets checked rows by the DataGridViewCheckBoxCell via it's name, in this case ProccessColumn, not the column index.

    Add this to your project

    Public Module Module1
        <Runtime.CompilerServices.Extension()>
        Public Function GetChecked(Grid As DataGridView, ColumnName As String) As List(Of DataGridViewRow)
            Return (From rows In Grid.Rows.Cast(Of DataGridViewRow)()
                    Where CBool(rows.Cells(ColumnName).Value) = True).ToList
        End Function
    End Module

    Form code (make sure the Visual Studio Output window is open)

    Public Class Form1
        Private Sub GetCheckedDataGridViewRowsButton_Click(sender As Object, e As EventArgs) _
            Handles GetCheckedDataGridViewRowsButton.Click
    
            Process(DataGridView1.GetChecked("ProcessColumn"))
    
        End Sub
        ''' <summary>
        ''' perform whatever you want to do
        ''' </summary>
        ''' <param name="rows"></param>
        Private Sub Process(rows As List(Of DataGridViewRow))
            For Each row As DataGridViewRow In rows
                Console.WriteLine(row.Cells("ComputerNameColumn").Value)
            Next
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(True, "A001")
            DataGridView1.Rows.Add(False, "B001")
            DataGridView1.Rows.Add(True, "C001")
            DataGridView1.Rows.Add(False, "D001")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jason Spatz Friday, September 20, 2019 5:55 PM
    Friday, September 20, 2019 4:57 PM
    Moderator

All replies

  • I would recommend using a DataGridViewCheckBoxColumn

    The extension gets checked rows by the DataGridViewCheckBoxCell via it's name, in this case ProccessColumn, not the column index.

    Add this to your project

    Public Module Module1
        <Runtime.CompilerServices.Extension()>
        Public Function GetChecked(Grid As DataGridView, ColumnName As String) As List(Of DataGridViewRow)
            Return (From rows In Grid.Rows.Cast(Of DataGridViewRow)()
                    Where CBool(rows.Cells(ColumnName).Value) = True).ToList
        End Function
    End Module

    Form code (make sure the Visual Studio Output window is open)

    Public Class Form1
        Private Sub GetCheckedDataGridViewRowsButton_Click(sender As Object, e As EventArgs) _
            Handles GetCheckedDataGridViewRowsButton.Click
    
            Process(DataGridView1.GetChecked("ProcessColumn"))
    
        End Sub
        ''' <summary>
        ''' perform whatever you want to do
        ''' </summary>
        ''' <param name="rows"></param>
        Private Sub Process(rows As List(Of DataGridViewRow))
            For Each row As DataGridViewRow In rows
                Console.WriteLine(row.Cells("ComputerNameColumn").Value)
            Next
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(True, "A001")
            DataGridView1.Rows.Add(False, "B001")
            DataGridView1.Rows.Add(True, "C001")
            DataGridView1.Rows.Add(False, "D001")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jason Spatz Friday, September 20, 2019 5:55 PM
    Friday, September 20, 2019 4:57 PM
    Moderator
  • Thank you for the reply!  That was a little much for my limited understanding .. but I grabbed what I needed  from your code and it worked for what I needed it to do :) Thank you so much!

    For Each row As DataGridViewRow In rows

    and

    (row.Cells("ComputerNameColumn").Value)


    Friday, September 20, 2019 5:55 PM