datagrid show data in custom way

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


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

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

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


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

How can I show only the Name, the matchScore 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


            Dim tmp As Integer = 0


            For i = 0 To Points.Length - 1


                tmp = tmp + Points(i)


            Next


            TotalScore = tmp




        End Set


    End Property


End Class




6 Replies

DM Dhanasekar Mohanraj Syncfusion Team September 1, 2022 02:03 PM UTC

Hi Matteo,

We regret to let you know that there are no possibilities to combine two different class properties data to bound together as an ItemsSource of SfDataGrid. If you have two classes with the inheritance(like base class and derived class) and you need some of the properties only then the rest properties will be restricted through the AutoGenerateField DataAnnotation attribute or not define the particular columns while generating manually. For more information related to DataBinding and column generation, please refer to the below user guide documentation link,

UG Link:

https://help.syncfusion.com/wpf/datagrid/data-binding ,

https://help.syncfusion.com/wpf/datagrid/columns#manually-defining-columns ,

https://help.syncfusion.com/wpf/datagrid/columns#exclude-column

Please let us know if you have any concerns about this.

Regards,
Dhanasekar M.



MA Matteo September 1, 2022 03:53 PM UTC

As you can see from the table I drawed I need to display only property of player object.

Player object as many property, and I want to show only some of them, and point that is one of theme is an array of integer that can have different length based on how the user defined it. I want a column for each item index of the array.



DM Dhanasekar Mohanraj Syncfusion Team September 2, 2022 02:00 PM UTC

Hi Matteo,


You can achieve your requirement by using the indexer property binding to the SfDataGrid shown below,

<Syncfusion:SfDataGrid x:Name="datagrid"                   

                       AllowEditing="True"

                       AutoGenerateColumns="False" >

    <Syncfusion:SfDataGrid.Columns>

        <Syncfusion:GridTextColumn MappingName="C1" />

        <Syncfusion:GridNumericColumn MappingName="C3[0]" />

        <Syncfusion:GridNumericColumn MappingName="C3[1]" />

        <Syncfusion:GridNumericColumn MappingName="C3[2]" />

    </Syncfusion:SfDataGrid.Columns>

</Syncfusion:SfDataGrid>


We have prepared the sample based on your scenario. For more information related to binding the indexer properties, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/wpf/datagrid/data-binding#binding-indexer-properties


Sample Explanation:

  1. We have created the Model with String, integer, and a list of nullable integer properties

  2. And we have given the inputs for all properties in ViewModel and set some values as null in the list as you mentioned some point data will be null

  3. And restring the integer data without defining it in column generation


Please find the sample in the attachment and let us know if you have any concerns about this.

Regards,

Dhanasekar M.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample_6a39dde5.zip


MA Matteo replied to Dhanasekar Mohanraj September 5, 2022 01:05 PM UTC

Basically you are using a list of object (that are integer), instead of using a property as integer()?

I can't use xaml mappingname as you suggest because the number of point can different when we declare a new championship. So now is 5 but I can initalize it at 15 or 20 ecc..  You declare as this:

 List<TestData> item = new List<TestData>();

            item.Add(new TestData() { C1 = "1", C2 = 1, C3 = new List<int?> { 1, null , null } });

            item.Add(new TestData() { C1 = "2", C2 = 2, C3 = new List<int?> { 2, 22, null }});

            item.Add(new TestData() { C1 = "1", C2 = 3, C3 = new List<int?> { 1, 78, 34 } });

            item.Add(new TestData() { C1 = "1", C2 = 4, C3 = new List<int?> { 2, 23, 34 } });

            item.Add(new TestData() { C1 = "1", C2 = 5, C3 = new List<int?> { 3, 23, 23 } });

            return item;

Since my data came from another source, they are not hard coded, the number of int in the list is taken from a property of Datas( in your example), or calculated if you prefer.




DM Dhanasekar Mohanraj Syncfusion Team September 6, 2022 02:34 PM UTC

Hi Matteo,

Currently, we are checking the feasibility to achieve your requirement. We will check and update you with further details on September 08, 2022.

We appreciate your patience until then.

Regards,
Dhanasekar M.



VS Vijayarasan Sivanandham Syncfusion Team September 8, 2022 02:37 PM UTC

Hi Matteo,

Your requirement is to add columns to the DataGrid based on the number of items in the list of integer collections in each row by iterating all the items in the Underlying collection and adding the columns in code behind as shown in the following code example.

//here get the ItemsSource

var itemsSourceCollection = (this.datagrid.DataContext as ViewModel).Datas

 

this.datagrid.ItemsSource = itemsSourceCollection;

 

var getIntegerListCount = 0;

 

foreach (var item in itemsSourceCollection)

{

    if (item.C3.Count > getIntegerListCount)                   

        getIntegerListCount = item.C3.Count;

}

 

//here create the number of integer column based on list Count

for (int i = 0; i < getIntegerListCount; i++)

{

    this.datagrid.Columns.Add(new GridNumericColumn()

    {

        MappingName = "C3[" + i + "]",

        UseBindingValue = true,

    });

}


Please find the sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample_63524b9.zip

Loader.
Up arrow icon