Solved

Combine all rows in datagridview

Posted on 2009-05-13
4
457 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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