Share via

Visual Basic database programming. using ADO and datagril controls. My program is for a "Tailor Business".

Syed Zubair Ali 1 Reputation point
2021-04-15T19:12:50.82+00:00

I want that if i book a order of customer of multiple shirts with his name what can i do for multiple rows in datagrid control .like merge cells of excel. customer name in a row while his order of two shirts in two colounms. like below example

Customer Name Order.


Ali Shirt 1
Shirt 2

Developer technologies | Windows Presentation Foundation
Developer technologies | VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,351 Reputation points
    2021-04-16T05:35:38.73+00:00

    Hi,
    you can use ListBox in DataGridTemplateColumn for Orders. Try following demo:

    XAML:

    <Window x:Class="Window089"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp1.WpfApp089"  
            mc:Ignorable="d"  
            Title="Window089" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False">  
          <DataGrid.Columns>  
            <DataGridTextColumn Header="Customer" Binding="{Binding Customer}"/>  
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>  
            <DataGridTemplateColumn Header="Orders">  
              <DataGridTemplateColumn.CellTemplate>  
                <DataTemplate>  
                  <ListBox ItemsSource="{Binding Orders}" DisplayMemberPath="OrderName"/>  
                </DataTemplate>  
              </DataGridTemplateColumn.CellTemplate>  
            </DataGridTemplateColumn>  
          </DataGrid.Columns>  
        </DataGrid>  
      </Grid>  
    </Window>  
    

    And classes:

    Imports System.Collections.ObjectModel  
    Imports System.ComponentModel  
      
    Namespace WpfApp089  
      Public Class ViewModel  
        Public Sub New()  
          LoadData  
        End Sub  
        Private cvs As New CollectionViewSource  
        Public ReadOnly Property View As ICollectionView  
          Get  
            Return cvs.View  
          End Get  
        End Property  
      
        Private Sub LoadData()  
          Dim col As New ObservableCollection(Of Data)  
          For i = 1 To 10  
            Dim d As New Data With {.Customer = $"Customer {i}", .Name = $"Name {i}"}  
            For k = 1 To 5  
              d.Orders.Add(New [Order] With {.OrderName = $"Order {i}-{k}"})  
            Next  
            col.Add(d)  
          Next  
          cvs.Source = col  
        End Sub  
      End Class  
      
      Public Class Data  
        Public Property Customer As String  
        Public Property Name As String  
        Public Property Orders As New ObservableCollection(Of Order)  
      End Class  
      
      Public Class Order  
        Public Property OrderName As String  
      End Class  
    End Namespace  
    

    Result:

    88445-x.png

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.