PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2012 Concatenate Columns in grid-VBForums
Results 1 to 4 of 4

Thread: Concatenate Columns in grid

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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?

    Parksie

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,565

    Re: Concatenate Columns in grid

    After concatenating, will you put the result back into the "part" column, or somewhere else?

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Concatenate Columns in grid

    Quote Originally Posted by vbfbryce View Post
    After concatenating, will you put the result back into the "part" column, or somewhere else?
    The concatenated text will go in the "part" column.

    Parksie

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Concatenate Columns in grid

    Quote Originally Posted by venerable bede View Post
    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?

    I am probably totally off base, but see if this gives you some ideas:
    VB.Net Code:
    1. Public Class Form1
    2.  
    3.    Private dt As New DataTable
    4.    Dim bs As New BindingSource
    5.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.       With dt
    7.          .TableName = "fred"
    8.          .Columns.Add("Name", GetType(String))
    9.          .Columns.Add("Part", GetType(String))
    10.          .Columns.Add("Part1", GetType(String))
    11.          .Columns.Add("Part2", GetType(String))
    12.          .Rows.Add(New Object() {"joe", "123", "", "dsd"})
    13.          .Rows.Add(New Object() {"fred", "123", "fd", "dsd"})
    14.          .Rows.Add(New Object() {"jill", "123", "33", "dsd"})
    15.       End With
    16.       bs.DataSource = dt
    17.  
    18.       ' process existing rows
    19.       For Each row As Object In bs.List
    20.          Concat(DirectCast(row, DataRowView))
    21.       Next
    22.  
    23.       AddHandler bs.ListChanged, AddressOf bs_ListChanged ' handle new rows and changes
    24.  
    25.       DataGrid1.DataSource = bs
    26.    End Sub
    27.  
    28.    Private Sub Concat(ByVal row As DataRowView)
    29.       Dim part As String = row("Part").ToString
    30.       Dim part1 As String = row("Part1").ToString
    31.       Dim part2 As String = row("Part2").ToString
    32.       If Not String.IsNullOrEmpty(part1) OrElse Not String.IsNullOrEmpty(part2) Then
    33.          part = String.Concat(part, part1, part2)
    34.          row("Part") = part
    35.          row("Part1") = DBNull.Value
    36.          row("Part2") = DBNull.Value
    37.       End If
    38.    End Sub
    39.  
    40.    Private Sub bs_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs)
    41.  
    42.       Select Case e.ListChangedType
    43.          Case System.ComponentModel.ListChangedType.ItemAdded, System.ComponentModel.ListChangedType.ItemChanged
    44.             Dim row As DataRowView = DirectCast(bs.List.Item(e.NewIndex), DataRowView)
    45.             If row.Row.RowState <> DataRowState.Detached Then Concat(row)
    46.       End Select
    47.  
    48.    End Sub
    49.  
    50. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width