How to get Type from bound datagridview?

Hobbyist_programmer 621 Reputation points
2021-05-19T10:07:04.017+00:00

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

Developer technologies | VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-05-19T12:12:14.327+00:00

    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
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.