Concatenate Columns in grid
I have a datagrid(Windows) which is bound to a BindingSource.
The columns in the bindingsource are "Name", "Part", "Part1", "Part2".
Part will always have an entry.
If Part1 and Part2 contain anything then I need to concatenate them to Part.
Normally I would just do this in sql or loop through a datatable before binding and do it that way but alas I am not allowed to touch these.
I must either loopthrough the BindingSource and perform the concatenation there before binding OR do the checks and concatenation in the Windows grid. Sadly the windows grid does not have a rowbound event so I have no idea how to progress.
Anyone have any suggestions?
Re: Concatenate Columns in grid
After concatenating, will you put the result back into the "part" column, or somewhere else?
Re: Concatenate Columns in grid
Quote:
Originally Posted by
vbfbryce
After concatenating, will you put the result back into the "part" column, or somewhere else?
The concatenated text will go in the "part" column.
Re: Concatenate Columns in grid
Quote:
Originally Posted by
venerable bede
I have a datagrid(Windows) which is bound to a BindingSource.
The columns in the bindingsource are "Name", "Part", "Part1", "Part2".
Part will always have an entry.
If Part1 and Part2 contain anything then I need to concatenate them to Part.
Normally I would just do this in sql or loop through a datatable before binding and do it that way but alas I am not allowed to touch these.
I must either loopthrough the BindingSource and perform the concatenation there before binding OR do the checks and concatenation in the Windows grid
What difference does it make if you access the underlying DataTable directly or via the BindingSource List items? :confused:
I am probably totally off base, but see if this gives you some ideas:
VB.Net Code:
Public Class Form1
Private dt As New DataTable
Dim bs As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With dt
.TableName = "fred"
.Columns.Add("Name", GetType(String))
.Columns.Add("Part", GetType(String))
.Columns.Add("Part1", GetType(String))
.Columns.Add("Part2", GetType(String))
.Rows.Add(New Object() {"joe", "123", "", "dsd"})
.Rows.Add(New Object() {"fred", "123", "fd", "dsd"})
.Rows.Add(New Object() {"jill", "123", "33", "dsd"})
End With
bs.DataSource = dt
' process existing rows
For Each row As Object In bs.List
Concat(DirectCast(row, DataRowView))
Next
AddHandler bs.ListChanged, AddressOf bs_ListChanged ' handle new rows and changes
DataGrid1.DataSource = bs
End Sub
Private Sub Concat(ByVal row As DataRowView)
Dim part As String = row("Part").ToString
Dim part1 As String = row("Part1").ToString
Dim part2 As String = row("Part2").ToString
If Not String.IsNullOrEmpty(part1) OrElse Not String.IsNullOrEmpty(part2) Then
part = String.Concat(part, part1, part2)
row("Part") = part
row("Part1") = DBNull.Value
row("Part2") = DBNull.Value
End If
End Sub
Private Sub bs_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs)
Select Case e.ListChangedType
Case System.ComponentModel.ListChangedType.ItemAdded, System.ComponentModel.ListChangedType.ItemChanged
Dim row As DataRowView = DirectCast(bs.List.Item(e.NewIndex), DataRowView)
If row.Row.RowState <> DataRowState.Detached Then Concat(row)
End Select
End Sub
End Class