Solved

Combine all rows in datagridview

Posted on 2009-05-13
4
455 Views
Last Modified: 2013-11-27
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
Comment
Question by:Benvor
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
rdoran0928 earned 500 total points
ID: 24375435
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
 

Author Comment

by:Benvor
ID: 24383141
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
 
LVL 4

Expert Comment

by:rdoran0928
ID: 24384528
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
 

Author Comment

by:Benvor
ID: 24384699
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question