[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Combine all rows in datagridview

Hi experts

I have a datagrid on my form and I need a user to be able to combine all the lines in the datagrid. I can already get it working but only when a user selects 2 lines. And the big problem is the cells where it is text must concatenate with the "&" and the Integer cells must plus the numbers together. I would greatly appreciate any help.

Thanx in advance
Dim result As String = String.Empty
        Me.DataGridView1.SelectAll()
        Dim k, l As Integer
        k = (Me.DataGridView1.Rows.Count - 1)
 
        For p As Integer = 0 To k
            l = k - p
            For i As Integer = 0 To Me.DataGridView1.SelectedRows(1).Cells.Count
                If IsNumeric(Me.DataGridView1.SelectedRows(l).Cells(i).Value) Then
                    Me.DataGridView1.SelectedRows(l).Cells(i).Value += Me.DataGridView1.SelectedRows(l + 1).Cells(i).Value
                Else
                    If Me.DataGridView1.SelectedRows(l).Cells(i).Value = Me.DataGridView1.SelectedRows(l).Cells(i).Value Then
                        Me.DataGridView1.SelectedRows(l).Cells(i).Value = Me.DataGridView1.SelectedRows(l + 1).Cells(i).Value
                    ElseIf Not Me.DataGridView1.SelectedRows(l).Cells(i).Value = Nothing Then
                        Me.DataGridView1.SelectedRows(l).Cells(i).Value += " & " + Me.DataGridView1.SelectedRows(l + 1).Cells(i).Value
                    Else
                        Return
                    End If
 
                End If
 
            Next
        Next

Open in new window

Preview.JPG
0
Benvor
Asked:
Benvor
  • 2
  • 2
1 Solution
 
rdoran0928Commented:
This should do what you want:

Dim selectedRows as DataGridViewSelectedRowCollection =  Me.DataGridView1.SelectedRows
Dim selectedRowCount as Integer = selectedRows.Count

If selectedRowCount > 1 then

    For i As Integer = 1 to Me.DataGridView1.Rows.Count  1

        For j as Integer = 0 to Me.DataGridView1.SelectedRows(0).Cells.Count

          If TypeOf (Me.DataGridView1.SelectedRows(i).Cells(j).Value) Is String Then

                Me.DataGridView1.SelectedRows(0).Cells(j).Value += " & " + Me.DataGridView1.SelectedRows(i).Cells(j).Value
            Else
                Me.DataGridView1.SelectedRows(0).Cells(j).Value += Me.DataGridView1.SelectedRows(i).Cells(j).Value
            End If
        Next
    Next
End If
0
 
BenvorAuthor Commented:
Hi rdoran0928

Your code works 100%, except that I needed to change the 4th line of code from ".Rows.Count" to ".SelectedRows.Count". But now I just need to know something else. I don't want the user to go and manually select all the rows in the datagrid, so I put a SellectAll() in the front of my code. But this also selects the last empty row witch causes problems. Is there a way to automatically select all rows with data in or just all the rows except for the last empty row?

Benvor
Preview.JPG
0
 
rdoran0928Commented:
Just loop through all of the rows and select each one.

    Dim allRows as DataGridViewRowCollection =  Me.DataGridView1.Rows

    For i As Integer = 0 to Me.DataGridView1.Rows.Count - 1

        allRows(i).Selected = True

    Next

0
 
BenvorAuthor Commented:
Here is the code for anyone who has a problem like this in the future. Combine all rows in a datagrid and delete all but the last row
        Dim allRows As DataGridViewRowCollection = Me.DataGridView1.Rows
 
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 2
            allRows(i).Selected = True
        Next
 
        Dim selectedRows As DataGridViewSelectedRowCollection = Me.DataGridView1.SelectedRows
        Dim selectedRowCount As Integer = selectedRows.Count
        If selectedRowCount > 1 Then
 
            For i As Integer = 0 To Me.DataGridView1.SelectedRows.Count - 1
                For j As Integer = 0 To Me.DataGridView1.SelectedRows(0).Cells.Count - 1
                    If IsNumeric(Me.DataGridView1.SelectedRows(i).Cells(j).Value) Then
                        Me.DataGridView1.SelectedRows(0).Cells(j).Value += Me.DataGridView1.SelectedRows(i).Cells(j).Value
                    Else
                        If Me.DataGridView1.SelectedRows(i).Cells(j).Value = Me.DataGridView1.SelectedRows(i).Cells(j).Value Then
                            Me.DataGridView1.SelectedRows(0).Cells(j).Value = Me.DataGridView1.SelectedRows(i).Cells(j).Value
                        Else
                            Me.DataGridView1.SelectedRows(0).Cells(j).Value += " & " + Me.DataGridView1.SelectedRows(i).Cells(j).Value
                        End If
                    End If
                Next
            Next
 
            Dim allRows2 As DataGridViewRowCollection = Me.DataGridView1.Rows
            For i As Integer = 0 To Me.DataGridView1.Rows.Count - 3
                DataGridView1.Rows.Remove(Me.DataGridView1.Rows(0))
            Next
 
        End If

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now