Bind only some property to a datagrid

Ajeje Brazorf 106 Reputation points
2022-09-01T18:10:50.997+00:00

I've this 2 class that allow me to manage a competition.

every competition can have nMatch Matches, with nTarget Targets.

I want to display on a datagrid something like that whit a row for any player

Where TX are the Points property.

+----------+----+---+----+----+-----+----+

+Name +T1 +T2 +T3 +T4 + T5 + TOT+

+----------+----+---+----+----+-----+----+

My problem is that if I bind the a collection of player with the datagrid it as itemsource it will display also the other property columns that I won't

How can I show only the Name, the matchScore(TOT) and the targets(T1 to 5 in the example but can change) values( 0-1 value only allowed).

I need also 2 way binding because if the user edit data in datagrid it should reflect on the collection.

Public Class Game  
  
    Private _Name As String  
  
    Public Property Name As String  
  
        Get  
  
            Return _Name  
  
        End Get  
  
        Set(ByVal value As String)  
  
            _Name = value  
  
        End Set  
  
    End Property  
  
    Private _nMatch As Integer  
  
    Public Property nMatch As Integer  
  
        Get  
  
            Return _nMatch  
  
        End Get  
  
        Set(ByVal value As Integer)  
  
            _nMatch = value  
  
        End Set  
  
    End Property  
  
    Private _nTarget As Integer  
  
    Public Property nTarget As Integer  
  
        Get  
  
            Return _nTarget  
  
        End Get  
  
        Set(ByVal value As Integer)  
  
            _nTarget = value  
  
        End Set  
  
    End Property  
  
End Class  
  
  
  
Public Class Player  
         
    Private _name As String  
    Public Property Name As String  
  
        Get  
                Return _name  
            End Get  
  
        Set(ByVal value As String)  
         _name = value  
    End Set  

End Property

    Private _matchScore As Integer()  
       Public Property matchScore As Integer()  
            Get  
                Return _matchScore  
            End Get  
            Set(ByVal value As Integer())  
                _matchScore = value  
            End Set  
        End Property  
  
    Private _totalScore As Integer  
  
    Public Property TotalScore As Integer  
            Get  
                Return _totalScore  
            End Get  
  
        Set(ByVal value As Integer)  
  
            _totalScore = value  
  
        End Set  

    End Property  
   
    Private _points As Integer()  
  
    Public Property Points As Integer()  
            Get  
  
            Return _points  

        End Get  

        Set(ByVal value As Integer())  
            _points = value  
        End Set  
   
    End Property  
 End Class  

Sorry for my bad english, if I do not expose the problem in a clair way please let me know, I'll try to improve.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,601 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
771 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ajeje Brazorf 106 Reputation points
    2022-09-02T10:02:46.487+00:00

    For a championship of 2 match(or stage), with 5 target for every match I do that:

     Public Property myCompetition As Game  
      myCompetition = New Game With {.Name = "This is a test competition", .nMatch = 2, .nTarget =5}  
      
     Dim GamePlayers As New List(Of Player)  
            GamePlayers.Add(New Player With {.Name = "A", .TotalScore = 7, .matchScore = {4, 3}, .Points = {1, 0, 1, 1, 1, 0, 1, 1, 1, 0}})  
            GamePlayers.Add(New Player With {.Name = "B", .TotalScore =8, .matchScore = {4, 4}, .Points = {0, 1, 1, 1, 1, 1, 0, 1, 1, 1}})  
            GamePlayers.Add(New Player With {.Name = "C", .TotalScore = 8, .matchScore = {4,4}, .Points =   {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}})  
    

    And I would show them in 2 datagrid as this:
    237294-grid1.png

    237295-grid1.png

    0 comments No comments