What is the usage of binding a datagridview to an arraylist full of for example some employee objects in c#?
I have 2 questions:
1- We all know that can create an array list full of some employee objects and bind a datagridview to it.
but is that way have some advantages to other ways?
2- using 开发者_StackOverflow社区the way above, how can we get the employee object info when a user clicks on a row of the datagridview?
thank you
Binding the grid to a strongly typed object rather than something like a DataTable helps reduce the errors in your code when you need to work with the object because of the compiler checks. In order to get the strongly typed object when a user clicks on row you can use the RowEvent as shown below:
Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
Dim Persons As List(Of Person) = CType(Me.DataGridView1.DataSource, List(Of Person))
Dim SelectedPerson As Person = Persons(e.RowIndex)
MsgBox(SelectedPerson.Name)
End Sub
精彩评论