?
Solved

Combine all rows in datagridview

Posted on 2009-05-13
4
Medium Priority
?
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
rdoran0928 earned 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 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