0

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?

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

I already have a data type class with public property for each column of DataGrid (I've added it in my question above). The problem is that, if I implement the INotifyPropertyChanged in that class, it raises an exception at startup, because of loop on field isPointSelectionEnded. How do I implement the interface in proper way? Notice that the I do the work between different classes.
In my answer, I provided you with a link to the INotifyPropertyChanged page at MSDN (2nd line)... click it and follow the help on that page.
If I implement the INotifyPropertyChanged, and in setter change the value to "Not ok", the value changes but the grid is not automatically upadated...and I see the old value...is it good to do the refresh of items? I ask it because randomly it raises an exception like "Refresh is not allowed during an AddNew or EditItem transaction"
When you implement the INotifyPropertyChanged interface correctly, your public properties should call the INotifyPropertyChanged.PropertyChanged event after they are set. This will 'refresh' that field in the UI. Therefore set the value using the public property to ensure that the event is called.
I've done it but the UI does not refresh the value...someone tells me that can be a problem of binding...I update code in the question about it
0

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>

1 Comment

Question is based on values in other columns
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.