none
Refresh datagrid

    Question

  • Hi every body!

    I dont know if Im in the correct forum.

    Im traying to refresh a datagrid that has like datasource a entity framework. The problem is when i pull a new object in the database i dont know how to pull inmediatly this new register in the datagrid becuese if i want the see that i have to close the page and open it again.

    thx!

    this is my code!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Navigation;
    using SaludProfesionales.Web;
    using SaludProfesionales.Web.Services;

    namespace SaludProfesionales.Views
    {
    public partial class Page1 : Page
    {
    ProfesionalDomainContext Pcd;
    PROFESIONAL ps;
    public Page1()
    {
    InitializeComponent();
    }


    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void pROFESIONALDomainDataSource_LoadedData(object sender, System.Windows.Controls.LoadedDataEventArgs e)
    {

    if (e.HasError)
    {
    System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
    e.MarkErrorAsHandled();
    }
    }
    private void btnGuardar_Click(object sender, RoutedEventArgs e)
    {
    if (primer_nombreTextBox.Text != "" && primer_apellidoTextBox.Text != "" && cedulaTextBox.Text != "" && ubicacionTextBox.Text != "" && fecha_nacimientoDatePicker.Text != "" && fecha_ingresoDatePicker.Text != "" && telefono_fijoTextBox.Text != "" && ciudadTextBox.Text != ""&&paisTextBox.Text!=""&&departamentoTextBox.Text!="")
    {
    if (GuardarProfesional())
    {
    MessageBox.Show("Los datos se han guardado");
    limpiar();
    }
    else
    {
    MessageBox.Show("No se han podido guardar los datos");
    }
    }
    else
    {
    MessageBox.Show("Debe llenar todo los datos obligatorios");
    }
    }
    public bool GuardarProfesional()
    {
    Pcd = new ProfesionalDomainContext();
    ps = new PROFESIONAL();
    ps.Primer_nombre = primer_nombreTextBox.Text;
    ps.Segundo_nombre = segundo_nombreTextBox.Text;
    ps.Primer_apellido = primer_apellidoTextBox.Text;
    ps.Segundo_apellido = segundo_apellidoTextBox.Text;
    ps.Sexo = sexoComboBox.SelectionBoxItem.ToString();
    ps.RH = rHTextBox.Text;
    ps.Telefono_fijo = telefono_fijoTextBox.Text;
    ps.Telefono_movil = telefono_movilTextBox.Text;
    ps.Ubicacion = ubicacionTextBox.Text;
    ps.Cedula = cedulaTextBox.Text;
    ps.Ciudad = ciudadTextBox.Text;
    ps.Codigo_postal = codigo_postalTextBox.Text;
    ps.Departamento = departamentoTextBox.Text;
    ps.Direccion_casa = direccion_casaTextBox.Text;
    ps.Direccion_trabajo = direccion_trabajoTextBox.Text;
    ps.Email = emailTextBox.Text;
    ps.Calificacion_profesional = calificacion_profesionalComboBox.SelectionBoxItem.ToString();
    ps.Fecha_ingreso = Convert.ToDateTime(fecha_ingresoDatePicker.Text);
    if (fecha_retiroDatePicker.Text != "")
    ps.Fecha_retiro = Convert.ToDateTime(fecha_retiroDatePicker.Text);
    else
    ps.Fecha_retiro = null;
    ps.Fecha_nacimiento = Convert.ToDateTime(fecha_nacimientoDatePicker.Text);
    ps.Proveedor_id = Convert.ToInt32(proveedor_idComboBox.SelectedValue);
    ps.Estado_civil = estado_civilComboBox.SelectionBoxItem.ToString();
    ps.Estado = estadoCheckBox.IsChecked;
    ps.Pais = paisTextBox.Text;
    Pcd.PROFESIONALs.Add(ps);

    if (Pcd.SubmitChanges().IsComplete == false)
    {
    return true;
    }
    else
    return false;

    }
    public void limpiar()
    {
    primer_nombreTextBox.Text = "";
    segundo_nombreTextBox.Text = "";
    primer_apellidoTextBox.Text = "";
    segundo_apellidoTextBox.Text = "";
    ciudadTextBox.Text = "";
    fecha_ingresoDatePicker.Text = "";
    fecha_nacimientoDatePicker.Text = "";
    fecha_retiroDatePicker.Text = "";
    telefono_fijoTextBox.Text = "";
    telefono_movilTextBox.Text = "";
    cedulaTextBox.Text = "";
    ubicacionTextBox.Text = "";
    rHTextBox.Text = "";
    paisTextBox.Text = "";
    departamentoTextBox.Text = "";
    codigo_postalTextBox.Text = "";
    emailTextBox.Text = "";
    direccion_casaTextBox.Text = "";
    direccion_trabajoTextBox.Text = "";
    }
    }
    Tuesday, April 03, 2012 7:26 PM

Answers

  • Hi rmadrid9006,

    Did you set DataGrid's ItemsSource as a DomainDataSource?

    If this is the case, you can re-load DomainDataSource in the callback method of SubmitChanges()

                 ...

                Pcd.PROFESIONALs.Add(ps);

               Pcd.SubmitChanges(callback=>

              {

                   [DomainDataSource].Load();

              }, null);
                {
                    return true;
                }
                else
                    return false;

    Best Regards,

    Thursday, April 05, 2012 10:44 PM

All replies

  • Im traying to refresh a datagrid that has like datasource a entity framework.

    Your question is not clear, let us know in detail.

    if you dont know when the new item/row is inserted into database, then how come you will make a call from application to get the data.

    or else for every 5 minutes or specific time make a call to database.

    let me know if any understood anything

    Wednesday, April 04, 2012 1:44 AM
  • Hi rmadrid9006,

    Did you set DataGrid's ItemsSource as a DomainDataSource?

    If this is the case, you can re-load DomainDataSource in the callback method of SubmitChanges()

                 ...

                Pcd.PROFESIONALs.Add(ps);

               Pcd.SubmitChanges(callback=>

              {

                   [DomainDataSource].Load();

              }, null);
                {
                    return true;
                }
                else
                    return false;

    Best Regards,

    Thursday, April 05, 2012 10:44 PM