none
Set values in mulitple selected rows of datagridview RRS feed

  • Question

  • Is it possible to do the following scenario:

    I have a grid that a user can multiselect rows from, I am looking to then set values withing those to be the same. I have a batch option that would take field 1, field 2 and set the values of all selected records in column 1 and column 2 to those values.

    Friday, August 31, 2018 5:32 PM

Answers

  • Here is an example.

    In a button click

    Dim selectCount = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
    If selectCount > 0 Then
        For i As Integer = 0 To selectCount - 1
            Console.WriteLine(DataGridView1.SelectedRows(i).Index.ToString())
            DataGridView1.SelectedRows(i).Cells(0).Value = "Karen"
        Next
    End If


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ryan Lashway Tuesday, September 4, 2018 1:29 PM
    Friday, August 31, 2018 6:04 PM
    Moderator

All replies

  • Hi

    Here is some example code that may provide you with enough information to deal with your requirements.

    The main point of interest would be the For Each .....in the Button click event where it shows how to loop through the selected rows.

    ' Form1 with DataGridView1 containing
    ' at least 4 columns (for
    ' this example only), and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      ' set up some random items
      ' to use in DGV replacement
      Dim par1 As String = "One"
      Dim par2 As String = "Two"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add some dummy data to DGV
        With DataGridView1
          For i As Integer = 1 To 10
            .Rows.Add(i, i * 2, i * 3, i * 4)
          Next
        End With
    
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each r As DataGridViewRow In DataGridView1.SelectedRows
    
          r.Cells(1).Value = par1
          r.Cells(3).Value = par2
    
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Friday, August 31, 2018 5:56 PM
  • Here is an example.

    In a button click

    Dim selectCount = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
    If selectCount > 0 Then
        For i As Integer = 0 To selectCount - 1
            Console.WriteLine(DataGridView1.SelectedRows(i).Index.ToString())
            DataGridView1.SelectedRows(i).Cells(0).Value = "Karen"
        Next
    End If


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Ryan Lashway Tuesday, September 4, 2018 1:29 PM
    Friday, August 31, 2018 6:04 PM
    Moderator
  • Perfect Karen, all the offered options were great but I ended up using Karen's as it worked with what I had for a process of checking the value in the cells and setting the color off the value, just didnt know how to tied setting multiple values to it.
    Tuesday, September 4, 2018 1:30 PM