WPF [SOLVED]: Datagrid Cell content is not visible in WPF

WPF [SOLVED]: Datagrid Cell content is not visible in WPF

Home Forums Frameworks WPF WPF [SOLVED]: Datagrid Cell content is not visible in WPF

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246869

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have a datagrid with 8 columns,concatenating the first 2 column values and bind it to the 7th column for every rows.[binding process is done in calendar closed event of the 6th column]In this after binding the 7th column value the bonded value is not visible in the 7 cell of the particular row but if i double clicked on the particular cell then the data is visible…I don’t know what is going wrong…

    This is the image of my Datagrid

    public class pojo
    {
        public string Prefix { get; set; }
        public int Year { get; set; }
        public int QuarterNo { get; set; }
        public int SerialNo { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public string PeriodName { get; set; }
    }
    
    ObservableCollection<pojo> models = new ObservableCollection<pojo>();
    
    private void Period_Name(object sender, RoutedEventArgs e)
    {
        var rowdata = calendarmstrDG.SelectedItem as pojo;
        var prefix = rowdata.Prefix;
        int year = rowdata.Year;
        int year_twodigits = year % 100;
        string sth = prefix + '-' + year_twodigits.ToString();
        pojo obj = (pojo)calendarmstrDG.SelectedItem;
        obj.PeriodName = sth;
    }
    

    Xaml code for 8th column

     <DataGridTextColumn Binding="{Binding Path=PeriodName}"  Width="85" IsReadOnly="True" Header="Period Name"/>
    

    #246870

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Class should look like this (other fields are omitted):

    public class pojo : INotifyPropertyChanged
    {
    
        private string _prefix;
        private int _year;
    
        public string Prefix
        {
            get => _prefix;
            set
            {
                if (_prefix == value) return;
                _prefix = value;
                OnPropertyChanged(nameof(PeriodName));
            }
        }
        public int Year
        {
            get => _year;
            set
            {
                if (_year == value) return;
                _year = value;
                OnPropertyChanged(nameof(PeriodName));
            }
        }
        public string PeriodName => _prefix + '-' + (_year % 100).ToString(); 
    
        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged(string prop)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }
    }
    

    Source: https://stackoverflow.com/questions/47950732/datagrid-cell-content-is-not-visible-in-wpf
    Author: JohnyL
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.