how to display content of cells base on value in C# WPF DataGrid ?

Mojtaba_Hakim 321 Reputation points
2022-04-12T15:01:55.85+00:00

I use C# WPF and SQL Server Database I have DataGrid with two columns wich fill with number

I have DataGrid and two columns, one number and one VASIYAT My VASIYAT column is filled with numbers from the database What I want to do is bet on what is the VASIYAT of the text you are displaying, given the value in this column. Example: There is a number 2 in the third cell of this column, so it should display the PreparingOrderState text in the final output in the Datagrid.

But my code does not work

 <DataGrid x:Name="hEAD_LSTDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10" RowDetailsVisibilityMode="VisibleWhenSelected">
                <DataGrid.Columns>

                    <DataGridTextColumn x:Name="nUMBERColumn" Binding="{Binding NUMBER}" Header="NUMBER" Width="SizeToHeader"/>

                    <DataGridTextColumn x:Name="tAMIRColumn" Binding="{Binding TAMIR}" Header="VASIYAT" Width="SizeToHeader">
                        <DataGridTextColumn.ElementStyle>

                            <Style TargetType="{x:Type TextBlock}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding  ElementName=TAMIR}" Value="2">
                                        <Setter Property="Text" Value="PreparingOrderState"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>

                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

                </DataGrid.Columns>
            </DataGrid>

What I need :
https://i.stack.imgur.com/S5b5g.png

Developer technologies Windows Presentation Foundation
Developer technologies XAML
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-04-13T10:07:21.17+00:00

    MainWindow.xaml:

    <Grid>  
            <DataGrid x:Name="hEAD_LSTDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"   
                      ItemsSource="{Binding}" Margin="10" RowDetailsVisibilityMode="VisibleWhenSelected">  
                <DataGrid.Columns>  
                    <DataGridTextColumn x:Name="nUMBERColumn" Binding="{Binding NUMBER}" Header="NUMBER" Width="SizeToHeader"/>  
                    <DataGridTextColumn x:Name="tAMIRColumn" Binding="{Binding Txt}"  Header="VASIYAT" />  
                </DataGrid.Columns>  
            </DataGrid>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Linq;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    
    namespace DataGridPropertyShowWithTrigger  
    {  
      public partial class MainWindow : Window  
      {  
        ViewModel vm=new  ViewModel();  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext=vm;  
          hEAD_LSTDataGrid.ItemsSource=vm.Datas;  
        }  
      }  
      public class ViewModel  
      {  
        public ObservableCollection<Data> Datas { get; set; }  
        public ViewModel()  
        {  
          Datas=new ObservableCollection<Data>();  
          Datas.Add(new Data(){ NUMBER=1, TAMIR=1 });  
          Datas.Add(new Data(){ NUMBER=2, TAMIR=2 });  
        }  
      }  
      public class Data : INotifyPropertyChanged  
      {  
    
        public int NUMBER { get;set;}  
        public event PropertyChangedEventHandler PropertyChanged;  
        private string txt;  
        public string Txt    
        {   
          get { return txt;}  
    
        }  
        private int Tamir;  
        public int TAMIR  
        {  
          get { return Tamir; }  
          set  
          {  
              Tamir = value;  
            if (Tamir == 2)  
            {  
              txt = "PreparingOrderState";  
            }  
            else  
            {  
              txt = Tamir.ToString();  
            }  
            OnPropertyChanged("Txt");  
            OnPropertyChanged("TAMIR");  
          }  
        }  
        protected void OnPropertyChanged([CallerMemberName] string name = null)  
        {  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
        }  
      }   
    }  
    

    The result:
    192673-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.
    0 comments No comments

  2. Ken Tucker 5,861 Reputation points
    2022-04-18T09:23:44.647+00:00

    You could use an IValueConverter also to format the output from a bound colum. In this case you could convert a number to status text

    https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Data.IValueConverter?view=winrt-22000

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.