Solved

Combine all rows in datagridview

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now