none
DotNet - DataGridView with custom DataTable as DataSource RRS feed

  • Question

  • Hi,

    I've a custom DataTable using as DataSource in an DataGridView.

    The DataTable contains only one column "Display Name".

    The DataTable rows was filled with instances of my custom class "CLASS_1".
    CLASS_1 contains properties "Name", "Location" and "ID" and member "ToString()". "ToString" will return the display name of an class instance.

    If i bind the DataTable as DataSource to the DataViewGrid, the grid shows the rows, each with one cell. The cell value is the return value of "ToString()" from the corresponding instance of CLASS_1.

    So far, so good.

    If interacting with an cell of this grid, in example mouse double click, a related event is fired. By this event parameters I can retrieve the corresponding cell using e.RowIndex and e.ColumnIndex.

    My Question is: How i can retriev the corresponding instance of CLASS_1?

    Emeriks

    Edit
    This is only an example! My real grid has a variable number of columns with variable names.



    • Edited by Emeriks Thursday, February 28, 2019 10:33 AM
    Wednesday, February 27, 2019 1:02 PM

All replies

  • If your underlying DataSource for the DataGridView is a DataTable consider the following.

    • Create a BindingSource form level e.g. Private bs = new BindingSource.
    • In place of setting the DataSource of the DataGridView to the DataTable do bs.DataSource = dt where dt is your DataTable. Next do someDataGridView.DataSource = bs.

    Now to get at data we can do the following.

    • Assert the current row is not nothing e.g. If bs.Current Isnot Nothing, if validate data we then do

    Dim Row As DataRow = CType(someBindingSource.Current, DataRowView).Row. This provides the DataRow. To look at a specific cell we don't ask the DataGridView, instead use Row.Field(Of String)("FirstName"), Field(Of T) works for all known types e.g. DateTime, Boolean, Double etc.

    If the DataRow has a pointer back to an instance of class_1 then you can then so this too.

    Say the DataGridView was loaded instead with a List(Of Class_1) you can cast BindingSource.Current to Class_1) and access properties of the class at this point.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, February 27, 2019 1:46 PM
    Moderator
  • Hello Karen,

    >> Say the DataGridView was loaded instead with a List(Of Class_1) you can cast BindingSource.Current to Class_1) and access properties of the class at this point.

    But this is'nt the same. In this case I've to use classes with a static number of properties and knowed names of these, one property for each column. But in my case I've variable number of columns with variable names.

    Thursday, February 28, 2019 10:29 AM