Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Datagrid selectedItem and databinding

$
0
0

I have two datagrid displayed on my UI. When I select a particular row on datagrid 1, I would like to display the details of the datagrid 1 on datagrid 2. I am populating the datagrid data from a database.
here is the two database table structure.

here is the code so far I have tried

Baseclass.cs

public class Baseclass
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void SetProperty<T>(ref T member, T value, [CallerMemberName] string propertyName = null)
    {
        member = value;
        this.RaiseNotification(propertyName);
    }

    protected void RaiseNotification(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

person.cs

public class person : Baseclass
{

    private int personID;
    public int PersonID
    {
        get { return personID; }
        set { this.SetProperty<int>(ref this.personID, value); }
    }

    private string firstName;
    public string FirstName
    {
        get { return firstName; }
        set { this.SetProperty<string>(ref this.firstName, value); }
    }

    private string lastName;
    public string LastName
    {
        get { return lastName; }
        set { this.SetProperty<string>(ref this.lastName, value); }
    }

    Model _personModel = new Model();
    private ObservableCollection<person> _person = new ObservableCollection<person>();
    public ObservableCollection<person> Getpersons
    {
        get { return _person; }
        set { _person = value; OnPropertyChanged("GetPersons"); }
    }

    public person()
    {
        initializeload();
    }

    private void initializeload()
    {
        try
        {
            DataTable table = _personModel.getData();

            for (int i = 0; i < table.Rows.Count; ++i)
                Getpersons.Add(new person
                {
                 PersonID = Convert.ToInt32(table.Rows[i][0]),
                 FirstName = table.Rows[i][1].ToString(),
                 LastName = table.Rows[i][2].ToString(),
                });
        }

        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyname)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyname));
    }

    public class Model
    {
        public DataTable getData()
        {
            DataTable ndt = new DataTable();
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
            sqlcon.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Person].[dbo].[persons]", sqlcon);
            da.Fill(ndt);
            da.Dispose();
            sqlcon.Close();
            return ndt;
        }
    }

}

PersonDetail.cs

public  class PersonDetails : Baseclass
{
    private int personID;
    public int PersonID
    {
        get { return personID; }
        set { this.SetProperty<int>(ref this.personID, value); }
    }

    private string address;
    public string Address
    {
        get { return address; }
        set { this.SetProperty<string>(ref this.address, value); }
    }

    private string pos;
    public string Position
    {
        get { return pos; }
        set { this.SetProperty<string>(ref this.pos, value); }
    }

    DetailsModel _detailModel = new DetailsModel();
    private ObservableCollection<PersonDetails> _details = new ObservableCollection<PersonDetails>();
    public ObservableCollection<PersonDetails> GetDetails
    {
        get { return _details; }
        set { _details = value; OnPropertyChanged("GetDetails"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyname)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyname));
    }

    public PersonDetails()
    {
        initializeload();
    }

    private void initializeload()
    {
        try
        {
            DataTable table = _detailModel.getData();

            for (int i = 0; i < table.Rows.Count; ++i)
                GetDetails.Add(new PersonDetails
                {
                    PersonID = Convert.ToInt32(table.Rows[i][0]),
                    Address = table.Rows[i][1].ToString(),
                    Position = table.Rows[i][2].ToString(),                        
                });
        }

        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
    public class DetailsModel
    {
        public DataTable getData()
        {
            DataTable ndt = new DataTable();
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
            sqlcon.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Person].[dbo].[personDetails]", sqlcon);
            da.Fill(ndt);
            da.Dispose();
            sqlcon.Close();
            return ndt;
        }
    }
}

XAML

<Grid><DataGrid Margin="100,20,116,211" ItemsSource="{Binding person}" SelectedItem="{Binding Selectedperson}"  /><DataGrid Margin="100,130,116,101" ItemsSource="{Binding personDetails}" /></Grid>

can anybody help to proceed in writing the MainViewModel? I am stuck here since weeks.


Beauty is within what you see & feel !


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>