Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combine rows in a DataGrid

Posted on 2009-05-08
2
Medium Priority
?
225 Views
Last Modified: 2013-11-27
Hi Experts

I have a datagrid on my form that I populate from a Access database and I recently got some code from EE to combine rows with each other and also combine the text of that rows with each other. But this only works for the first column, and only if it is a text value. What I actually want to do is plus the other rows as well, witch are integers, and when I combine 2 rows it must delete one of the rows. Can any one please help me out here or give me some sample code or refer me to a useful website. Any help would be greatly appreciated

Thanx in advance
Dim result As String = String.Empty
 
        If (DataGridView1.SelectedCells.Count > 1) Then
 
 
            For Each cell As DataGridViewCell In Me.DataGridView1.SelectedCells
 
                result &= cell.Value.ToString & " & "
 
 
            Next
            result = result.Remove(result.Length - 2)
 
            Me.DataGridView1.SelectedCells(0).Value = result.ToString()
        End If

Open in new window

Before.JPG
After.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 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 2000 total points
ID: 24334173
It looks to me like your only doing cells - you need to use the SelectedRows property so that you can do the whole row.
This should get you close:

        If (DataGridView1.SelectedRows.Count > 1) Then
 
            For i As Integer = 0 To Me.DataGridView1.SelectedRows(0).Cells.Count - 1
                Me.DataGridView1.SelectedRows(0).Cells(i).Value += " & " + Me.DataGridView1.SelectedRows(1).Cells(i).Value
            Next
 
            DataGridView1.Rows.Remove(Me.DataGridView1.SelectedRows(1))
        End If

Open in new window

0
 

Author Comment

by:Benvor
ID: 24334276
Thanx allot GuitarRich. I just had to put this IF in to combine the cells that are Integer. Here is the final code to combine rows in a datagrid.
        If (DataGridView1.SelectedRows.Count > 1) Then
 
            For i As Integer = 0 To Me.DataGridView1.SelectedRows(0).Cells.Count - 1
                If IsNumeric(Me.DataGridView1.SelectedRows(0).Cells(i).Value) Then
                    Me.DataGridView1.SelectedRows(0).Cells(i).Value += Me.DataGridView1.SelectedRows(1).Cells(i).Value
                Else
                    Me.DataGridView1.SelectedRows(0).Cells(i).Value += " & " + Me.DataGridView1.SelectedRows(1).Cells(i).Value
                End If
                'Me.DataGridView1.SelectedRows(0).Cells(i).Value += " & " + Me.DataGridView1.SelectedRows(1).Cells(i).Value
            Next
 
            DataGridView1.Rows.Remove(Me.DataGridView1.SelectedRows(1))
        End If

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 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