How can I get the start row and end row of selected cells in datagridview

Daniel McElhinney 120 Reputation points
2023-02-23T07:58:23.0166667+00:00

I'm having trouble finding any examples of getting the start row and end row of a selected cells in a Datagridview. The selected cells will be in the same column of the Datagridview

The column header text is like this:

Draw | Val1 | Val2 | Val3

 Assuming that I have a 100 rows of data and I have selected rows 30 to 50 in the Val2 column. I would like to get the value from the Draw column for row 30 (Start Row) and the value of the Draw column for row 50 (End Row).

Unfortunately I haven’t been able to find any examples on line. Is there anyone who can help with this problem.

Developer technologies | Windows Forms
Developer technologies | .NET | Other
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-02-23T10:00:37.22+00:00

    Check an approach:

    If DataGridView1.SelectedCells.Count > 0 Then
    
        Dim first_row_index As Integer = DataGridView1.SelectedCells.Cast(Of DataGridViewCell).Min(Function(c) c.RowIndex)
        Dim first_row As DataGridViewRow = DataGridView1.Rows(first_row_index)
    
        Dim last_row_index As Integer = DataGridView1.SelectedCells.Cast(Of DataGridViewCell).Max(Function(c) c.RowIndex)
        Dim last_row As DataGridViewRow = DataGridView1.Rows(last_row_index)
    
        Dim first_draw_value = first_row.Cells("Draw")
        Dim last_draw_value = last_row.Cells("Draw")
    
    End If
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Daniel McElhinney 120 Reputation points
    2023-02-23T11:30:02.12+00:00

    Hi thanks for you answer Viorel. I've tried the example you posted however I keep getting an error with the last two statements in the if block

    Dim first_draw_value = first_row.Cells("Draw")

    Dim last_draw_value = last_row.Cells("Draw")

    I switch out ("Draw") with all the different column names including one called ("Game") however the same error keeps popping up.

    Message=Column named Game cannot be found.

    System.ArgumentException
      HResult=0x80070057
      Message=Column named Game cannot be found.
    Parameter name: columnName
      Source=System.Windows.Forms
      StackTrace:
       at System.Windows.Forms.DataGridViewCellCollection.get_Item(String columnName)
       at PowerBallDB.Form1.Button2_Click(Object sender, EventArgs e) in C:\Users\user\source\repos\PowerBallDB\PowerBallDB\Form1.vb:line 102
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at PowerBallDB.My.MyApplication.Main(String[] Args) in :line 83
    
      This exception was originally thrown at this call stack:
        [External Code]
        PowerBallDB.Form1.Button2_Click(Object, System.EventArgs) in Form1.vb
        [External Code]
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.