Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagriId bound to an observable collection. I want to store the correct values in the observable collection (with all decimals) but I want to see less decimals in the datagrid. So I tried this:

http://stackoverflow.com/questions/12164079/change-datagrid-cell-value-programmatically-in-wpf[^]

and some similar others.

In the end what I need is to change the value of the datagrid when the event is fired.

C#
private void Datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
  DataGridRow row = e.Row;
  var pePCDmise = row.Item as PcDmisData.Measures;

  DataGridRow rowContainer = dtgResults.GetRow(0);
  DataGridCell  d = dtgResults.GetCell(rowContainer, 3);
}


So the rowcontainer above is not null but when I try to get the cell value I get a null exception. Particularly:

C#
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
  if (row != null)
  {
    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

    if(presenter == null)
    {
      grid.ScrollIntoView(row, grid.Columns[column]);
      presenter = GetVisualChild<DataGridCellsPresenter>(row);
    }

    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
    return cell;
  }
  return null;
}


the presenter above is null and it's null also after having entered the if.
How can I make it work?

Aside from the aforementioned problem how can I make a one-way-bind? I have set the datagrid bind with

dtgResults.ItemsSource = easyRunData.olstMeasures;

but now I want to change the dtgResults number of decimals only and not the observable collection value.

Thanx for any help
Patrick
Posted

1 solution

I think you're going the hard way. I think it's a lot simpler to add in the class type your ObservableCollection is another field with a field containing the less decimals string and show that in your grid. Something like:
C#
privatec ObservableCollection<dataclass> _list;
public ObservableCollection<dataclass> list
{
    get
       {
           return this._list;
       }
    set
       {
           this._list = value;
           RaisePropertyChanged("list");
       }
}

public class DataClass
{
    public double val { get;set; }
    public string formattedVal 
    {
        get
           {
            return this.val.ToString("#0.00");
           }

    }
}


Another way could be using DataGridTemplateColumn.
 
Share this answer
 
v2

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