How to edit data (numbers only) and calculate its values in a DataGrid?

6

I need a DataGrid column to receive users only numbers in the rows (cells) and after clicking on the button, multiplication calculations are performed between cells on the same row and different columns. The DataGrid is populated as follows to the class below through the LINQ tool ... The users column is the "No. Stops", it needs to be multiplied by the "Time (min)" column.

But the values that the user types in the cells do not stay in them , what is typed is disappearing . As soon as it leaves the cell the value is erased, why?

And as for the calculation logic, I'm only able to add the cells of a column.

Who can help thank you. Below I put the sequence of commands ...

LINQ Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TRSSystem.AcessoDados
{
    public class tabMaquinaParadaAcesso
    {
//Consultar pela MAQUINA no Banco pela estrutura LINQ TO SQL Server
        public static List<tabMaquinaParada> Consultar_OnlyMaquina(string pMaquina, string pTipo)
        {
            TRSSystemDataClassesDataContext oDB = new TRSSystemDataClassesDataContext();
            List<tabMaquinaParada> aMaquina = (from Selecao in oDB.tabMaquinaParadas where Selecao.Maquina == pMaquina && Selecao.Sacaria == pTipo select Selecao).ToList<tabMaquinaParada>();
            return aMaquina;

        }

    }
}

XMAL:

<DataGrid x:Name="dataGridPrdMaquina_ApontPrd" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="4,7,0,0" VerticalAlignment="Top" Height="103" Width="577" GridLinesVisibility="Horizontal" SelectionUnit="Cell" VerticalContentAlignment="Center" AutoGenerateColumns="False" SelectionMode="Single" AlternatingRowBackground="{DynamicResource SelectedBackgroundBrush}" HorizontalGridLinesBrush="{DynamicResource MouseOverBrush}" VerticalGridLinesBrush="{DynamicResource MouseOverBrush}" HorizontalContentAlignment="Center">
    <DataGrid.Columns>
              <DataGridTextColumn Header="Código" Width="Auto" Binding={Binding CodigoParada}" IsReadOnly="True"/>
              <DataGridTextColumn Header="Descrição da Parada" Width="Auto" Binding="{Binding DescricaoParada}" IsReadOnly="True"/>
              <DataGridTextColumn Header="Tempo (min)" Width="Auto" Binding="{Binding TempoParada}" IsReadOnly="True"/>
              <DataGridTextColumn Header="Tipo" Width="Auto" Binding="{Binding TipoParada}" IsReadOnly="True"/>
              <DataGridTextColumn Header="Nº Paradas" Width="Auto" IsReadOnly="False" FontFamily="Calibri"> 
                                   <DataGridTextColumn.EditingElementStyle>
                                                <Style TargetType="TextBox">
                                                    <Setter Property="MaxLength" Value="2"/>
                                                </Style>
                                            </DataGridTextColumn.EditingElementStyle>
                                        </DataGridTextColumn>
                                    </DataGrid.Columns>
                                </DataGrid>

LostFocus event of a Combox, when the list is loaded:

private void CarregarParadas_ApontaPrd(object sender, RoutedEventArgs e)
        {
            try
            {
               dataGridPrdMaquina_ApontPrd.ItemsSource = TRSSystem.AcessoDados.tabMaquinaParadaAcesso.Consultar_OnlyMaquina(CmBox_MaquinaApontaPrd.Text, CmBox_TipoApontaPrd.Text);
            }
            catch(Exception error)
            {
                MessageBox.Show("Erro de Compilação, contacte o Administrador do Sistema." + error, "Erro de Compilação", MessageBoxButton.OK, MessageBoxImage.Error);
            }


        }

PreviewTextInput Event - Get Integer Only

private void SomenteInt_ListViewPrdPadrao_ApontaPrd(object sender, TextCompositionEventArgs e)
        {
            if (!char.IsDigit(e.Text, e.Text.Length - 1))
                e.Handled = true;
        }

Calculation Button

How do I multiply the values in the Time column by the number of Stops?

//Aqui apenas Soma os valores de uma coluna apenas:
 private void CalcularTempoEfetivo_ApontPrd(object sender, RoutedEventArgs e)
        {

            int somarColuna = 0;

            for (int i = 0; i < dataGridPrdMaquina_ApontPrd.Items.Count; i++)
            {

                tabMaquinaParada Dados = (tabMaquinaParada)dataGridPrdMaquina_ApontPrd.Items[i];

                somarColuna = somarColuna + Dados.TempoParada;

            }

            txtTempoEfetivo_ApontaPrd.Text = Convert.ToString(somarColuna);

        }
    
asked by anonymous 27.05.2016 / 01:31

1 answer

2

The problem is that you need to bind with a property.

As you are using a Linq class, create a column in the tabMaquinaParada table only for the system to accept this new field. *

  
  • This operation is not the best.
  •   

Table

Create the field NroParada as integer;

Desing

In the desing of class TRSSystemDataClasses add the new field.

Datagrid

And in%% of the number of stops does the binding:

<DataGridTextColumn Header="Nº Paradas" Width="Auto" IsReadOnly="False" FontFamily="Calibri" Binding="{Binding NroParada}">...</DataGridTextColumn >

Calculate

And with that you end up solving your 2 question:

    private void CalcularTempoEfetivo_ApontPrd(object sender, RoutedEventArgs e)
    {

        int somarColuna = 0;

        for (int i = 0; i < dataGridPrdMaquina_ApontPrd.Items.Count-1; i++)
        {

            tabMaquinaParada Dados = (tabMaquinaParada)dataGridPrdMaquina_ApontPrd.Items[i];

            somarColuna = somarColuna + (Dados.TempoParada*Dados.NroParada );

        }

        txtTempoEfetivo_ApontaPrd.Text = Convert.ToString(somarColuna);

    }
  

It is necessary to make datagrid because the system has a blank line, if it does not, the system has an error when converting.

    
27.05.2016 / 02:28