Question

I have a Datagrid with some columns. It is binded to a list of objects. One of the column is a text column: I need it as a "validation" column, infact I want that, for each row, the value of this cell is "OK" or "NOT OK", based on values present in other cells. I don't know how to write a string into a certain cell into the Datagrid. Any suggestion?

EDIT: following the class that describes the objects of DataGrid

public class BracketProperty
{
    [XmlAttribute]
    public int index { get; set; }
    public BracketType type { get; set;}
    public List<BracketPoint> bracketPointsList;
    [XmlIgnore]
    public String isPointSelectionEnded { get; set; }
}

EDIT2: someone tells me that this line of code is not good

this.BracketsDataGrid.ItemsSource = this.currentPropertyToShow.sides[this.sideIndex - 1].brackets;

where brackets is defined as

 public ObservableCollection<BracketProperty> brackets;

because is not a binding...how can I change it to be a binding?

Was it helpful?

Solution

The easiest way to do this is to create a data type class that has a public property for each column in the DataGrid. You should implement the INotifyPropertyChanged interface in this class.

Then you can have a property which you could update in a PropertyChanged handler:

private void YourDataType_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (Property1 == "SomeValue" && Property2 > 0) ValidationProperty = "OK";
    else ValidationProperty = "NOT OK"; // Add your own condition above of course
}

In the constructor:

PropertyChanged += YourDataType_PropertyChanged;

OTHER TIPS

Use Value Converters

public partial class MainWindow : Window
{
    public ObservableCollection<Employee> EmpList { get; set; }
    public MainWindow()
    {

        InitializeComponent();
        this.DataContext = this;
        EmpList = new ObservableCollection<Employee> { 
            new Employee(1, "a"), 
            new Employee(2, "b"), 
            new Employee(3, "c") };
    }
}

public class NumValueConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int v = int.Parse(value.ToString());
        if (v < 3) 
            return "YES";
        else 
            return "NO";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class Employee
{
    public int Salary { get; set; }
    public string Name { get; set; }

    public Employee(int s, string n)
    {
        Salary = s;
        Name = n;
    }
}

XAML

<Window x:Class="garbagestack.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cv="clr-namespace:garbagestack"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <cv:NumValueConverter x:Key="cv1"/>
        </Grid.Resources>
        <DataGrid AutoGenerateColumns="False" Margin="12,99,12,12" Name="dg1" ItemsSource="{Binding EmpList}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="NewValue" Binding="{Binding Salary}"/>
                <DataGridTextColumn Header="NewValue" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="NewValue" Binding="{Binding Salary,Converter={StaticResource cv1}}">

                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

I need to answer to question on my own. I have not implemented any INotifyPropertyChanged interface or Converter...the only code added, after setting the value of the variable for control, is the following:

 this.DataGrid.Items.Refresh();

and text is correctly shown in the grid...

However, it is better to implement the INotifyPropertyChanged, following the MVVM pattern rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top