Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf DataGrid only showing empty rows

so i am working on the app which retrieves data from the database and i want to display it in DataGrid with custom columns which i created.

XAML:

<DataGrid x: Name = "dataGridAddedAgents" Grid.Column = "0" Grid.ColumnSpan = "7"  ItemsSource = "{Binding}"  HorizontalAlignment = "Stretch" Margin = "10,0" Grid.Row = "3" VerticalAlignment = "Top" AutoGenerateColumns = "False"  IsReadOnly = "True" >
<DataGrid.Columns>            
    <DataGridTextColumn Binding = "{Binding Path=Id}"  Width = "0.25*"  Header = "Id" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />                    
    <DataGridTextColumn Binding = "{Binding Path=FirstName}" Width = "0.15*"  Header = "First name" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />                                         
    <DataGridTextColumn Binding = "{Binding Path=LastName}" Width = "0.15*"  Header = "Last name" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />                                                     
    <DataGridTextColumn Binding = "{Binding Path=Email}"  Width = "0.20*"  Header = "Email" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />                                                               
    <DataGridTextColumn Binding = "{Binding Path=Username}"  Width = "0.15*" Header = "Username" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />                        
    <DataGridTextColumn Width = "0.10*" ClipboardContentBinding = "{x:Null}" Header = "Remove" CanUserResize = "False" CanUserReorder = "False" Foreground = "{x:Null}" />                                                                                     
</DataGrid.Columns>                                                                  
</DataGrid>

and here is c# code where i try to put data into DataGrid

response = await client.GetAsync("api/user/agents");
        listOfAgents = await response.Content.ReadAsAsync<ICollection<ApplicationUserModel>>();                        
        dataGridAddedAgents.ItemsSource = listOfAgents;

I have check with debugger and i load data correctly into listOfAgents. When i run program DataGrid shows correct number of rows but all cells are empty.

edit:

 public class ApplicationUserModel
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string GuestIdentification { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

Class ApplicationUserModel looks like this. i use it too write into database and it works Soulutin:

removing Foreground = "{x:Null}" from columns in DataGrid fixed it. thanks @mm8 for the help

like image 814
rykereve Avatar asked Jun 05 '26 01:06

rykereve


1 Answers

When i run program DataGrid shows correct number of rows but all cells are empty.

Then you need to make sure that the ApplicationUserModel class contains public properties (and not fields) named "Id", "FirstName", "LastName", "Email" and "Username" and that these properties actaully return the expected values.

Each property should at least have a public setter for you to be able to bind to them:

public class ApplicationUserModel
{
   public string Id {get; set; }
   public string FirstName {get; set; }
   ...
}

Edit: You shold also remove Foreground="{x:Null}" from your DataGridTextColumns.

By the way the ItemsSource that you set programmatically will "overwrite" the one you bind to in your XAML markup.

like image 143
mm8 Avatar answered Jun 07 '26 20:06

mm8