Home > Mobile >  Problem with displaying data in DataGrid using C# in WPF
Problem with displaying data in DataGrid using C# in WPF

Time:12-20

I am trying to display some dummy data in a DataGrid in my Shortlist.xaml window, however, the datagrid is empty when I run the app.

enter image description here

My code in Shortlist.xaml.cs is as follows:

namespace WpfApp_Employment_Help
{
    public partial class Shortlist : Window
    {
     
        public ObservableCollection<ShortlistedClient> clients { get; set; } = new ObservableCollection<ShortlistedClient>();


        public Shortlist()
        {
            InitializeComponent();
            DataContext = clients;
            clients.Add(new ShortlistedClient("Rich", "07111118265", "[email protected]", "Glasgow", "Office", "MSc", "more than 3 years", "Yes", "No"));
            clients.Add(new ShortlistedClient("Steve", "07567718265", "[email protected]", "Glasgow", "Construction", "High School", "more than 3 years", "Yes", "No"));
            clients.Add(new ShortlistedClient("Maria", "07485999005", "[email protected]", "Edinburgh", "Office", "MSc", "more than 3 years", "No", "No"));
        }

        private void lstShort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void dgr_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}

My Shortlist.xample code:


<Window x:Class="WpfApp_Employment_Help.Shortlist"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp_Employment_Help"
        mc:Ignorable="d"
        Title="Shortlist" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="dgr" d:ItemsSource="{d:SampleData ItemCount=5}" Margin="300,95,110,179" SelectionChanged="dgr_SelectionChanged"/>

    </Grid>
</Window>

My Client and ShortlistedClient classes are as follows:

namespace WpfApp_Employment_Help
{
    public partial class Client

    {
        private static int nextID = 0;
        public string ID { get; private set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Location { get; }
        public string Worktype { get; }
        public string Qualification { get; }
        public string Workexp { get; }
        public string Drlicence { get; }
        public string Crconviction { get; }
        public bool IDed { get; private set; }

        public Client(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc)
        {
            Name = n;
            Phone = p;
            Email = e;
            Location = l;
            Worktype = wt;
            Qualification = q;
            Workexp = we;
            Drlicence = dl;
            Crconviction = cc;

        }

        public void AssignClientID()
        {
            if (!IDed)
            {
                ID = nextID.ToString("D4");
                nextID  ;
                IDed = true;
            }
        }


 
    }
}


and

namespace WpfApp_Employment_Help
{
    
    public class ShortlistedClient : Client
    {

        public DateTime DT { get; private set; }
        public bool InterestedinVac { get; private set; }


        public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>();
        public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc)
        {
            DT = new DateTime();
            InterestedinVac = false;

        }
    }
}

In my MainWindow I have this method which activates the Shortlist.xaml. (as it opens a new window, Shortlist.xaml after a button click):

        private void GenShortlist_Click(object sender, RoutedEventArgs e)
        {
            Shortlist w = new Shortlist();
            w.Show();
        }

Can someone point out to me what's wrong?

CodePudding user response:

Since you are setting the DataContext of your window to an ObservableCollection<T>, you should bind the ItemsSource property of the DataGrid directly to it like this:

<DataGrid x:Name="dgr" d:ItemsSource="{d:SampleData ItemCount=5}"
    ItemsSource="{Binding}" Margin="300,95,110,179" SelectionChanged="dgr_SelectionChanged"/>
  • Related