-2

I am writing a simple program in C # using wpf, a semblance of a base, I understand that it would be easier to solve this problem using subd and entity framework, but the point is that you need to solve this way

So, I have a text file from which I need to load data into the date grid.

I have a class that describes a line from this text file, here it is:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string MidName { get; set; }
    public int Informatics { get; set; }
    public int Maths { get; set; }
    public int Physics { get; set; }
    public double Score { get; set; }
}

The scores field, for data from the program (the arithmetic mean of the student's grades)

And I have a datagrid in which I need to output data:

<DataGrid x:Name="DGridStudents" IsReadOnly="True" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="329" Margin="57,15,0,0" VerticalAlignment="Top" Width="736">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Width="*" Binding="{Binding Id}"/>
            <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="LastName" Width="*" Binding="{Binding LastName}"/>
            <DataGridTextColumn Header="MidName" Width="*" Binding="{Binding MidName}"/>
            <DataGridTextColumn Header="Informatics" Width="*" Binding="{Binding Informatics}"/>
            <DataGridTextColumn Header="Maths" Width="*" Binding="{Binding Maths}"/>
            <DataGridTextColumn Header="Physics" Width="*" Binding="{Binding Physics}"/>
            <DataGridTextColumn Header="Score" Width="*" Binding="{Binding Score}"/>
        </DataGrid.Columns>
    </DataGrid>

Thus, I used to fill the DataGrid, now I needed to add the Score field (Arithmetic mean of grades for subjects)

List<Student> list = new List<Student>();
            using (StreamReader sr = new StreamReader(fileName, true))
            {
                
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    var parsed = line.Split(' ');
                    list.Add(new Student
                        (
                           Convert.ToInt32(parsed[0]),
                           parsed[1],
                           parsed[2],
                           parsed[3],
                           Convert.ToInt32(parsed[4]),
                           Convert.ToInt32(parsed[5]),
                           Convert.ToInt32(parsed[6])
                        ));
                }
            }
            DGridStudents.ItemsSource = list;

Please tell me how can I do this using data bindings without using the MVVM pattern?

Example of a line from a text file:

1 Benedict Timothy-Carlton Cumberbatch 5 5 5

  • All you are asking is how to calculate the arithmetic mean of three integers? Sum them up and divide the result by three. – Clemens Feb 27 '21 at 19:14
  • XD, no, I'm asking how can I output the data to the date grid in such a way that it can be done via bindings @Clemens – roxyashechka Feb 27 '21 at 19:21
  • Instead of binding to Score, use a MultiBinding to the three other properties, with a Binding Converter that calculates the desired value. Otherwise just calculate the value in the Student constructor. – Clemens Feb 27 '21 at 19:29

2 Answers2

0
var student = new Student
                        {
                            Id = Convert.ToInt32(parsed[0]),
                            Name = parsed[1],
                            LastName = parsed[2],
                            MidName = parsed[3],
                            Informatika = Convert.ToInt32(parsed[4]),
                            Matematika = Convert.ToInt32(parsed[5]),
                            Fizika = Convert.ToInt32(parsed[6]),
                            Score = 5
                        };
                        list.Add(student);
0

If you're trying to concatenate the values of each columns then you can use MultiBinding.

Here's an example:

<DataGridTextColumn Header="Your Header">
    <DataGridTextColumn.Binding>
                        
        <!-- 
            Your string format here.
            This will put a space between each value.
        -->
        <MultiBinding StringFormat="{}{0} {1} {2} {3} {4} {5} {6} {7}">

            <!-- Your bindings... -->
            <MultiBinding.Bindings>
                <Binding Path="Id" />
                <Binding Path="Name" />
                <Binding Path="LastName" />
                <Binding Path="MidName" />
                <Binding Path="Informatics" />
                <Binding Path="Maths" />
                <Binding Path="Physics" />
                <Binding Path="Score" />
            </MultiBinding.Bindings>
        </MultiBinding>
    </DataGridTextColumn.Binding>

</DataGridTextColumn>

EDIT:

I think I misunderstood what the line from your text file was. If you want to perform some kind of calculation you can create a converter.

Here's an example of a math converter that has a property called Operation to determine what to do.

  • This converter is intended to perform math operations for 2 values only - You'll have to write your code to accommodate what you need.
public sealed class MathMultipleConverter : IMultiValueConverter
{
    #region Converter Properties
        
    public MathOperation Operation { get; set; }

    #endregion

    public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        //  Do you calculations here...

        if (value == null || value.Length < 2 || 
            value[0] == null || value[1] == null) 
            return Binding.DoNothing;

        if (!double.TryParse(value[0].ToString(), out double value1) || 
            !double.TryParse(value[1].ToString(), out double value2))
            return 0;

        switch (Operation)
        {
            default:
                return value1 + value2;
            case MathOperation.Divide:
                return value1 / value2;
            case MathOperation.Multiply:
                return value1 * value2;
            case MathOperation.Subtract:
                return value1 - value2;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Usage:

  1. Reference the converter in your resources somewhere. I set the Operation property to Multiply.
<UserControl.Resources>
        <converters:MathMultipleConverter x:Key="MathMultipleConverter" Operation="Multiply" />
</UserControl.Resources>
  1. Use it in your MultiBinding.
<DataGridTextColumn Header="Your Header">
    <DataGridTextColumn.Binding>

        <!-- Your Converter -->
        <MultiBinding Converter="{StaticResource MathMultipleConverter}">

            <!-- Your bindings... -->
            <MultiBinding.Bindings>
                <Binding Path="Maths" />
                <Binding Path="Physics" />
            </MultiBinding.Bindings>
        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>
Brxndxn
  • 189
  • 1
  • 7
  • I forgot to mention that if you meant to do some sort of calculation then you can create a converter and multibind to that instead of StringFormat. – Brxndxn Feb 27 '21 at 20:29