mercredi 16 août 2017

How to compare a TextBox content with dataGrid Cell content in WPF

I'm having a WPF Project in which I have CRUD. What happens is that I want my save button to behave as an update button too. Like, if I select a datagridrow in the datagrid, it will fill my textboxes with the datagridrow content and, if the user change some of its content, I want that when he clicks the 'Save' button the datagrid only saves the information he had updated. I made an if statement inside the Save button but the else if statement is being skipped. I reviewed my code for hours but can't see what am I doing wrong. My code in simple terms is as follows:

private void btnSalvar_Click(object sender, RoutedEventArgs e)
    {
        if(modeloVeiculo.Text == "" || placa.Text == "" || nomeMotorista.Text == ""
            || horaSaida.Text == "" || horaEntrada.Text == "" || data.Text == "")
        {
            //MessageBox customizado. Quando usar MessageBox, usar nesse formato. Se tiver alguma dúvida, a documentação está no site: http://ift.tt/2v414qc 
            MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("Alguns campos ainda estão em branco! Por favor, preencha todos os campos antes de pressionar o botão salvar.", "Aviso!", MessageBoxButton.OK, MessageBoxImage.Warning);
        }
        else if(modeloVeiculo.Text == dataGrid.SelectedCells.ToString() || placa.Text == dataGrid.SelectedCells.ToString() || nomeMotorista.Text == dataGrid.SelectedCells.ToString()
            || horaSaida.Text == dataGrid.SelectedCells.ToString()
            || horaEntrada.Text == dataGrid.SelectedCells.ToString() || data.Text == dataGrid.SelectedCells.ToString())
        {
            tbControleVeiculosFrota.modeloveiculo = modeloVeiculo.Text;
            tbControleVeiculosFrota.placa = placa.Text;
            tbControleVeiculosFrota.motorista = nomeMotorista.Text;
            var hSaida = TimeSpan.Parse(horaSaida.Text);
            tbControleVeiculosFrota.horasaida = hSaida;
            var hEntrada = TimeSpan.Parse(horaEntrada.Text);
            tbControleVeiculosFrota.horaentrada = hEntrada;
            var dataDT = DateTime.Parse(data.Text);
            tbControleVeiculosFrota.data = dataDT;
            db.SaveChanges();
            MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("Editado com sucesso!", "Aviso!", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        else
        {
            tbControleVeiculosFrota.modeloveiculo = modeloVeiculo.Text;
            tbControleVeiculosFrota.placa = placa.Text;
            tbControleVeiculosFrota.motorista = nomeMotorista.Text;
            var hSaida = TimeSpan.Parse(horaSaida.Text);
            tbControleVeiculosFrota.horasaida = hSaida;
            var hEntrada = TimeSpan.Parse(horaEntrada.Text);
            tbControleVeiculosFrota.horaentrada = hEntrada;
            var dataDT = DateTime.Parse(data.Text);
            tbControleVeiculosFrota.data = dataDT;
            db.tbControleVeiculosFrota.Add(tbControleVeiculosFrota);
            db.SaveChanges();
            MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("Rotina efetuada com sucesso!", "Sucesso!", MessageBoxButton.OK, MessageBoxImage.Information);
            modeloVeiculo.Text = "";
            placa.Text = "";
            nomeMotorista.Text = "";
            horaSaida.Text = "";
            horaEntrada.Text = "";
            data.Text = "";
        }
    }

The most strange is that is not giving me any errors. As you can see, I'm really away from being an expert in C# and WPF either. Any help is appreciated; thanks for the attention.

Aucun commentaire:

Enregistrer un commentaire