Based on the links given by Graeme (you should read an entire tutorial, not just the top part), use an 'if' statement to check if there are any employees added to the list, if not your index will be '0', if there are, count the items and add 1 to the count which will give you the proper index number -
Private Sub DGEmployeeList_AddingNewItem(sender As Object, e As AddingNewItemEventArgs) Handles DGEmployeeList.AddingNewItem
Dim newEmployee As New Employee()
If EmployeeList.Count > 0 Then
newEmployee.IndexNo = EmployeeList.Max(Function(emp) emp.IndexNo) + 1
Else
newEmployee.IndexNo = 0
End If
e.NewItem = newEmployee
End Sub
[EDIT]
If the above code error with message - 'System.Invalid.Operation: An ItemsControl is inconsistent with its item source', it means that the exception encountered is caused by attempting to modify the EmployeeList directly while it is bound to the DataGrid. To overcome this error, you can use an 'ObservableCollection(Of T)' instead of a 'List(Of T)' for EmployeeList -
MS Learn ObservableCollection<t> Class[
^]
You can adapt your code to -
Imports System.Collections.ObjectModel
Class MainWindow
Sub New()
InitializeComponent()
EmployeeList = New ObservableCollection(Of Employee)()
EmployeeList.Add(New Employee With {
.IndexNo = 1,
.employeeID = 7354,
.employeeName = "John Smith",
.employeeAddress = "122 made up Lane",
.employeeCity = "Cameden",
.employeeState = "New Jersey"
})
EmployeeList.Add(New Employee With {
.IndexNo = 2,
.employeeID = 9456,
.employeeName = "Andy Coesfoe",
.employeeAddress = "345 Green Street",
.employeeCity = "Salt Lake",
.employeeState = "Utah"
})
DGEmployeeList.ItemsSource = EmployeeList
End Sub
Partial Class Employee
Public Property IndexNo As Integer
Public Property employeeID As Integer
Public Property employeeName As String
Public Property employeeAddress As String
Public Property employeeCity As String
Public Property employeeState As String
End Class
Public EmployeeList As ObservableCollection(Of Employee)
Private Sub DGEmployeeList_AddingNewItem(sender As Object, e As AddingNewItemEventArgs) Handles DGEmployeeList.AddingNewItem
Dim newEmployee As New Employee()
If EmployeeList.Count > 0 Then
newEmployee.IndexNo = EmployeeList.Max(Function(emp) emp.IndexNo) + 1
Else
newEmployee.IndexNo = 0
End If
e.NewItem = newEmployee
End Sub
End Class
[EDIT 2]
Based on comments below regarding the update of 'IndexNo' when a row is delted -
To handle a row that were deleted, you can listen to the 'CollectionChanged' event of the 'ObservableCollection' and reassign the correct index values to each item whenever a change occurs. Alternatively, you can use a 'ListCollectionView' to sort the collection based on the 'IndexNo' property.
Your code will look similar to -
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Class MainWindow
Sub New()
InitializeComponent()
EmployeeList = New ObservableCollection(Of Employee)()
EmployeeList.Add(New Employee With {
.IndexNo = 1,
.employeeID = 7354,
.employeeName = "John Smith",
.employeeAddress = "122 made up Lane",
.employeeCity = "Camden",
.employeeState = "New Jersey"
})
EmployeeList.Add(New Employee With {
.IndexNo = 2,
.employeeID = 9456,
.employeeName = "Andy Coesfoe",
.employeeAddress = "345 Green Street",
.employeeCity = "Salt Lake",
.employeeState = "Utah"
})
Dim view As ListCollectionView = New ListCollectionView(EmployeeList)
view.SortDescriptions.Add(New SortDescription("IndexNo", ListSortDirection.Ascending))
DGEmployeeList.ItemsSource = view
AddHandler EmployeeList.CollectionChanged, AddressOf EmployeeList_CollectionChanged
End Sub
Partial Class Employee
Public Property IndexNo As Integer
Public Property employeeID As Integer
Public Property employeeName As String
Public Property employeeAddress As String
Public Property employeeCity As String
Public Property employeeState As String
End Class
Public EmployeeList As ObservableCollection(Of Employee)
Private Sub EmployeeList_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
Dim newIndex As Integer = 1
For Each emp As Employee In EmployeeList
emp.IndexNo = newIndex
newIndex += 1
Next
End Sub
Private Sub DGEmployeeList_AddingNewItem(sender As Object, e As AddingNewItemEventArgs) Handles DGEmployeeList.AddingNewItem
Dim newEmployee As New Employee()
If EmployeeList.Count > 0 Then
newEmployee.IndexNo = EmployeeList.Max(Function(emp) emp.IndexNo) + 1
Else
newEmployee.IndexNo = 1
End If
EmployeeList.Add(newEmployee)
e.NewItem = newEmployee
End Sub
End Class