Как суммировать данные datagridview
Я объединил данные столбца 1 и столбца 2 Datagridview, но теперь я хочу суммировать столбец 3, столбец 4 и столбец 5 в соответствии с объединенным столбцом.
Что я уже пробовал:
Private Sub DataGridView2_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView2.CellPainting If e.ColumnIndex = 0 AndAlso e.RowIndex <> -1 Then Using gridBrush As Brush = New SolidBrush(Me.DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor) Using gridLinePen As Pen = New Pen(gridBrush) e.Graphics.FillRectangle(backColorBrush, e.CellBounds) If e.RowIndex < DataGridView2.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1) End If e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom) If Not e.Value Is Nothing Then If e.RowIndex > 0 AndAlso DataGridView2.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then Else e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault) End If End If e.Handled = True End Using End Using End If If e.ColumnIndex = 1 AndAlso e.RowIndex <> -1 Then Using gridBrush As Brush = New SolidBrush(Me.DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor) Using gridLinePen As Pen = New Pen(gridBrush) e.Graphics.FillRectangle(backColorBrush, e.CellBounds) If e.RowIndex < DataGridView2.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1) End If e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom) If Not e.Value Is Nothing Then If e.RowIndex > 0 AndAlso DataGridView2.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then Else e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault) End If End If e.Handled = True End Using End Using End If If e.ColumnIndex = 4 AndAlso e.RowIndex <> -1 Then Using gridBrush As Brush = New SolidBrush(Me.DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor) Using gridLinePen As Pen = New Pen(gridBrush) e.Graphics.FillRectangle(backColorBrush, e.CellBounds) If e.RowIndex < DataGridView2.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1) End If e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom) If Not e.Value Is Nothing Then If e.RowIndex > 0 AndAlso DataGridView2.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then Else e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault) End If End If e.Handled = True End Using End Using End If If e.ColumnIndex = 2 AndAlso e.RowIndex <> -1 Then Using gridBrush As Brush = New SolidBrush(Me.DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor) Using gridLinePen As Pen = New Pen(gridBrush) e.Graphics.FillRectangle(backColorBrush, e.CellBounds) If e.RowIndex < DataGridView2.Rows.Count - 2 AndAlso DataGridView1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1) End If e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom) If Not e.Value Is Nothing Then If e.RowIndex > 0 AndAlso DataGridView2.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then Else e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault) End If End If e.Handled = True End Using End Using End If End Sub