question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked karenpayneoregon answered

How to get Type from bound datagridview?

Hallo,

Is there a way to get a object Type from a datagridview which bound to bindingsource with binding list?

I can get it , if the datagridview has atleat one row by following code but how to do it when it does not contain any row?

 Dim ObjType as Type = datGridView1.CurrentRow.DataBoundItem.GetType


Thanks

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Hello,

Knowing what type the BindingList is you then know the type for

Dim ObjType as Type = datGridView1.CurrentRow.DataBoundItem.GetType

But to check/verify something like this would be in order (and you can do else if if there are more possibilities). In the example below the BindingList is setup as List(Of Work)

 If DataGridView1.CurrentRow.DataBoundItem.GetType Is GetType(Work) Then
    
 End If

And to see this work here is a code sample.

 Imports System.ComponentModel
    
 Public Class Form1
    
     WithEvents workBindingSource As New BindingSource
     Private workBindingList As New BindingList(Of Work)
    
    
     Private Sub PopulateButton_Click(sender As Object, e As EventArgs) Handles PopulateButton.Click
    
         Dim list As New List(Of Work) From {
             New Work With {.Id = 1, .Description = "First", .UnitCost = 100, .Quantity = 2},
             New Work With {.Id = 2, .Description = "Second", .UnitCost = 10, .Quantity = 1},
             New Work With {.Id = 99, .Description = "Ninety nine", .UnitCost = 200, .Quantity = 1},
             New Work With {.Id = 100, .Description = "One hundred", .UnitCost = 100, .Quantity = 5}
         }
    
         workBindingList = New BindingList(Of Work)(list)
         workBindingSource.DataSource = workBindingList
         DataGridView1.DataSource = workBindingSource
    
     End Sub
    
     Private Sub TotalButton_Click(sender As Object, e As EventArgs) Handles TotalButton.Click
         If workBindingSource.DataSource IsNot Nothing Then
             Dim total = workBindingList.ToList().Total()
             TotalLabel.Text = $"Total: {total}"
         Else
             MessageBox.Show("No work items")
         End If
     End Sub
    
     Private Sub ChangeButton_Click(sender As Object, e As EventArgs) Handles ChangeButton.Click
    
         If workBindingSource.DataSource IsNot Nothing Then
             Dim quantity As Integer
    
             If Integer.TryParse(QuantityTextBox.Text, quantity) Then
                 Dim current As Work = workBindingList(workBindingSource.Position)
    
                 current.Quantity = quantity
                 workBindingSource.ResetCurrentItem()
             End If
    
         Else
             MessageBox.Show("No work items")
         End If
    
     End Sub
 End Class



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.