Home > Software design >  Why I cannot add Items into DataGrid in C#
Why I cannot add Items into DataGrid in C#

Time:07-26

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.

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; }
        }

CodePudding user response:

You could also try

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

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

CodePudding user response:

As mentioned, you can't use the indexing for a HashSet. If you're that insistent of using a HashSet, you can try alertToDataGrid.Add(Alert.alerts.ElementAt(i));. You'll need to have System.Linq included.

CodePudding user response:

You cannot use [] i.e. indexing operator on HashSet as it is unordered data structure which does not support indexing. So it is not about DataGrid but about HashSet insterad of for loop please use foreach loop to iterate over HashSet or any other IEnumerable which does not support indexing.

  • Related