0

How I can provide ComboBox to a particular DataGrid Column in wpf when user tries to edit data grid . For example user can change the Operator at run time from ">" , "<", "=", ">=", "<=". Also how can I stop user from entering negative values are in Value column

MainWindow.xaml.cs

namespace EditableDataGridApp
{
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            mydatagrid.ItemsSource = fields.Rule();
        }
    }
}

MainWindow.Xaml

  <Grid>
        <DataGrid Name="mydatagrid" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" IsReadOnly="True"></DataGridTextColumn>
                <DataGridTextColumn Header="Operator" Binding="{Binding Path=Operator}"></DataGridTextColumn>
                <DataGridTextColumn Header="Value" Binding="{Binding Path=Value}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

Field.cs

namespace EditableDataGridApp
{
    class fields
    {
        public string Name { get; set; }

        public string Operator { get; set; }

        public int Value { get; set; }


        public static ObservableCollection<fields> Rule()
        {
            ObservableCollection<fields> a = new ObservableCollection<fields>();
            a.Add(new fields() {Name = "length", Operator= "=", Value =5 });
            a.Add(new fields() { Name = "Width", Operator =">", Value = 6 });
             a.Add(new fields() { Name = "Height", Operator = "<", Value = 8 });
            return a;

        }
}
Leo
  • 11
  • 2
  • Put `wpf combobox in datagrid` in google : [wpf datagrid combobox column](https://stackoverflow.com/questions/19003133/wpf-datagrid-combobox-column) – Orace Apr 19 '22 at 07:50
  • Also. Put `wpf datagrid value validation` in google : [How to: Implement Validation with the DataGrid Control](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-implement-validation-with-the-datagrid-control) – Orace Apr 19 '22 at 07:53
  • And also. Read this [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Orace Apr 19 '22 at 07:55

1 Answers1

0

Combobox can be created by

<DataGridComboBoxColumn Header="Operator" x:Name="Operator" SelectedValueBinding="{Binding Path=Operator, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" ItemsSource="{Binding Operators}" ></DataGridComboBoxColumn>

Itemsource for ComboBox will be the list of all the elements you want in your dropdown.

public List<String> Operators;

 Operators = new List<string>();
            Operators.Add("=");
            Operators.Add("<");
            Operators.Add(">");
            Operators.Add(">=");
            Operators.Add("=<");

You can write this code of providing list of operators in xaml.cs file

Leo
  • 11
  • 2