TAGS :Viewed: 6 - Published at: a few seconds ago

[ WPF DataGrid not loading data ]

I am a beginner in C# and WPF . I have created a user control LogTable.atxml which contains a DataGrid and added it to the MainWindow.xaml . The Table is displayed but the contents are not being fetched. I think the issue is im not able to sent the Itemsource in the right way. [Result]Please help.

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

    namespace TableTest.UserControls
    {
    class Tabledata
    {
    string A{ get; set; }
    string B { get; set; }
    string C { get; set; }

    public Tabledata(string a, string b, string c) 
    {
    A = a;
    B = b;
    C =c;
    }

    }
    }



    namespace TableTest.UserControls
    {
    /// <summary>
    /// Interaction logic for LogTable.xaml
    /// </summary>
    public partial class LogTable : UserControl
    {

    ObservableCollection<Tabledata> list;
    public LogTable()
    {
    InitializeComponent();
    list = getTableDetails();
    this.logGrid.ItemsSource = list;
    }

    private ObservableCollection<Tabledata> getTableDetails()
    {
    ObservableCollection<Tabledata> list= new ObservableCollection<Tabledata>();
    Tabledata data = new Tabledata("aaa", "aaa", "aaa");
    Tabledata data1 = new Tabledata("bbb", "aaa", "aaa");
    Tabledata data2 = new Tabledata("ccc", "aaa", "aaa");
    list.Add(data);
    list.Add(data1);
    list.Add(data2);
    return list;
    }
    }
    }

    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    }
    }


<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:UserControls="clr-namespace:TableTest.UserControls"       x:Class="TableTest.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <UserControls:LogTable x:Name="logtable" HorizontalAlignment="Left" Margin="0,209,0,0" VerticalAlignment="Top" Width="287" Height="111"/>

    </Grid>
</Window>

<UserControl x:Class="TableTest.UserControls.LogTable"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 

             >
    <DataGrid x:Name="logGrid" AutoGenerateColumns="False" 
          Height="290" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" Width="290" 
             ItemsSource="{Binding list}"
          >
        <DataGrid.Columns >
            <DataGridTextColumn Binding="{Binding Path=A}" MinWidth="50" Header="Column 1"/>
            <DataGridTextColumn Binding="{Binding Path=B}" MinWidth="50" Header="Column 2"/>
            <DataGridTextColumn Binding="{Binding Path=C}" MinWidth="50" Header="Column 3"/>
        </DataGrid.Columns>
    </DataGrid>
</UserControl>

Answer 1


I think you need to do a few things 1. Your ItemSource needs to bind to a property. So your code should look something like

 public partial class LogTable : UserControl
    {

    public ObservableCollection<Tabledata> list {get;set;}
    public LogTable()
    {
    InitializeComponent();
    DataContext=this;
    list = new ObservableCollection<TableData>();
    list = getTableDetails();
    this.logGrid.ItemsSource = list;
    }
  1. You need to set your data context of your user control. If you are just using the codebehind you can get away with setting the DataContext in your usercontrols constructor like in the code above. But probably in the future you are going to want to use the mvvm pattern and set your datacontext to your viewmodel.

Note: You will need to set your datacontext of the mainwindow if you want to access any information from that window's codebehind (or whatever you want to bind data from).

Here is a good resource to read up on mvvm.

Update: Just saw your xaml. Since you named the Datagrid you can actually get away with not setting the DataContext as your as setting the ItemSource directly in your code. However, since you don't have your datacontext set you can remove the ItemSource={Binding list} from your xaml. That will only work if you have the list property available on your DataContext.

Update 2: You also need to make your properties public on your TableData class. then it will work

 class Tabledata
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }

    public Tabledata(string a, string b, string c)
    {
        A = a;
        B = b;
        C = c;
    }

}