Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My DataGrid is not showing values from a bound DataTable. Strangely, it still loads the number of rows needed to accomodate all the items in the DataTable.

Here's the model:

C#
private string _productkey;
private int _price, _stock;

public string ProductKey
{
     get {return _productkey;}
     set
       {
          if (_productkey != value)
             _productkey = value;

          OnPropertyChanged("ProductKey");
          OnPropertyChanged("Price");
          OnPropertyChanged("Stock");
       }
}

public int Price
{
     get {return _price;}
     set
       {
          if (_price != value)
             _price= value;

          OnPropertyChanged("Price");
       }
}

public int Stock
{
     get {return _stock;}
     set
       {
          if (_stock != value)
             _stock= value;

          OnPropertyChanged("Stock");
       }
}


In the DataGrid, I just want to show the ProductKey and price, Here is the ViewModel:

C#
private Product _product;
private DataTable dt;

public DataTable ProductPrices
{
     get
       {
          dt.Columns.Clear();
          dt.Rows.Clear();

          dt.Columns.Add("Product Key",string);
          dt.Columns.Add("Price",int);

          dt.Rows.Add("101",null);
          dt.Rows.Add("102",null);
          dt.Rows.Add("201",null);
          dt.Rows.Add("305",null);

          for (int i = 0 ; i < dt.RowCount ; i ++)
            {
               _product.ProductKey = dt.Rows[i][0].ToString();
               dt.Rows[i][1] = _product.Price;
            }

          return dt;
       }
}


finally, here's the xaml for my DataGrid
XML
<DataGrid ItemsSource="{Binding ProductPrice}">

</DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Binding="{x:Null}" ClipBoardContentBinding="{x:Null}" Header="Product Key" IsReadOnly="True"/>
<DataGridTextColumn Binding="{x:Null}" ClipBoardContentBinding="{x:Null}" Header="Price" IsReadOnly="True"/>
</DataGrid.Columns>


What did I miss?
Posted

1 solution

Your ItemsSource is looking for a property called "ProductPrice", but the view-model property is called "ProductPrices". I assume that's just a typo in your question?

The ProductPrices property looks odd - you're clearing and resetting the DataTable every time the property is accessed.

You're also using an instance of your model to read the price, but in the code you've posted, the price is never updated. Have you just omitted some of the code?

The main problem is most likely the bindings on the grid columns, which haven't been set:
XML
<DataGridTextColumn Binding="{x:Null}" ...

These need to be set to proper bindings for the columns you want to display:
XML
<DataGridTextColumn Binding="{Binding Path='Product Key'}" ... />
<DataGridTextColumn Binding="{Binding Path=Price}" ... />

NB: To bind to a column name that contains a space, you need to wrap the name in single quotes. Since the name isn't displayed anywhere, it would be easier to rename the column to ProductKey, without the space.

The code you've posted also has the DataGrid.Columns outside of the DataGrid tag. I assume that's another typo?
 
Share this answer
 
Comments
Lyandor 30-Sep-15 12:15pm    
Thanks for pointing that out, I completely missed out this part. It works perfectly now.

Yeah, I made the typo at the xml code, I didn't notice it at all.

Regarding the ProductPrices, I wrote it that way just for this example so I can keep it as short as possible.

I called OnPropertyChanged("Price") on set ProductKey property, this way, when I set a new ProductKey, the details will just be called right away and I can just get any of the attributes I need without setting them 1 by 1 based on the ProductKey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900