Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I cannot add Items into DataGrid in C#

I’m trying to dynamically add new records to DataGrid. I’m using HashSet to prevent duplicates. But when I want to add objects from HashSet to ObservableCollection, I get the error:

Cannot apply indexing with [] to an expression of type
‘system.collections.generic.ienumerable<>.

I have implemented IEnumarable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Contructor in AdminWindow.xaml.cs class

 public AdminWindow()
        {
            InitializeComponent();
            ObservableCollection<Alert> alertToDataGrid = new ObservableCollection<Alert>();
           
            for (int i = 0; i < Alert.alerts.Count; i++)
            {
                alertToDataGrid.Add(Alert.alerts[i]); //Here is the issue
            }

            AlertTable.ItemsSource = alertToDataGrid;
        }

Alert Class

public class Alert:IEnumerable<Alert>
        {
            private string id;
            private DateTime date;
            private string email;
            private string nameOfAlert;
            private string typeOfAlert; // will be enum later
            public static HashSet<Alert> alerts = new HashSet<Alert>();
    
            public override string ToString()
            {
                return id + "\n" + Date+  "\n" + email + "\n" + nameOfAlert + "\n" + typeOfAlert;
            }
    
            public override bool Equals(object obj)
            {
                return obj is Alert alert &&
                       id == alert.id &&
                       Id == alert.Id;
            }
    
            public override int GetHashCode()
            {
                return HashCode.Combine(id, Id);
            }
            //Tried just to return Enumerator
            public IEnumerator<Alert> GetEnumerator()
            {
                foreach (Alert alert in alerts)
                {
                    yield return alert;
                }
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
    
            public Alert(string id, DateTime date , string email, string nameOfAlert, string typeOfAlert)
            {
                this.Id = id;
                this.Date = date;
                this.Email = email;
                this.NameOfAlert = nameOfAlert;
                this.TypeOfAlert = typeOfAlert;
            }
    
            public string Id { get => id; set => id = value; }
            public string Email { get => email; set => email = value; }
            public string NameOfAlert { get => nameOfAlert; set => nameOfAlert = value; }
            public string TypeOfAlert { get => typeOfAlert; set => typeOfAlert = value; }
            public DateTime Date { get => date; set => date = value; }
        }

>Solution :

You could also try

foreach (Alert alert in Alert.alerts) {
  alertToDataGrid.Add(alert);
}

See How can I enumerate through a HashSet? for more details

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading