question

SyedZubairAli-5249 avatar image
0 Votes"
SyedZubairAli-5249 asked PeterFleischer-3316 answered

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

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

windows-wpfdotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

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



x.png (47.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.