Add column to a existing list of Objects ?

Hobbyist_programmer 621 Reputation points


I have list of objects bound to datagridview with bindingsource. is there a way to add temporary columns to list and modify values using Linq.

I can add a new column to datagridview using columns.add method but when i use LInq i dont know how to specify this column in linq to modify values.


An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,611 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Michael Taylor 49,251 Reputation points

    Knowing nothing else about your system I would say add the properties to your objects. If the values are calculated then make them a calculated property on the object (e.g age of a person given their birth date). If for some reason you cannot modify the actual type itself then consider using a model type instead that wraps the original data and provides a UI-specific view of it to make the UI easier.

    Alternatively you could add the column to the DGV directly and then using a binding expression to calculate the value on the fly as well.

    If you need to be able to have the user modify the value in the DGV then you have little choice but to add it to the object/model type that you're binding. If you don't then there would be no easy way to get/store the data permanently. While you could rely on the edit events this would be messy in my opinion.

    0 comments No comments

  2. Hobbyist_programmer 621 Reputation points

    Those columns has to be generated at runtime . I can not add properties before and the whole datagridview is read only and they are not saved. I have to show/bring the values programmatically.

    I can add column programmatically to datagridview. I have almost 300 rows in my list and i bring the values using Linq operations to list. In the same loop operation i can bring the values for the added column but i dont know how to cast the values from the linq operation to datagridview added column.

    In the worst case i have to make another loop just for the dgv incase if i dont find a better way. here is my sample code .

    How can i add lets say i value to the added column in the loop?..


    Public Class Form1  
        Public BS As New BindingSource  
        Public sampleList As New List(Of Item)  
        Private Sub SampleButton_Click(sender As Object, e As EventArgs) Handles SampleButton.Click  
            DataGridView1.Columns.Add("Col1", "New Column")  
            For i = 1 To 3  
                Dim c = sampleList.FirstOrDefault(Function(x) x.ID = i)  
                c.UnitCost = c.UnitCost * 2  
        End Sub  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            sampleList = Mocked.ItemList()  
            BS.DataSource = sampleList  
            DataGridView1.DataSource = BS  
        End Sub  
    End Class  
    Public Class Mocked  
        Public Shared Function ItemList() As List(Of Item)  
            Return New List(Of Item) From {  
                 New Item() With {.ID = 1, .Item = "A", .UnitCost = 5},  
                 New Item() With {.ID = 2, .Item = "B", .UnitCost = 10},  
                 New Item() With {.ID = 3, .Item = "C", .UnitCost = 15}  
        End Function  
    End Class  
    Public Class Item  
        Public Property ID As Integer  
        Public Property Item As String  
        Public Property UnitCost As Integer  
    End Class  
    0 comments No comments

  3. Hobbyist_programmer 621 Reputation points

    Ok. I found a solution

                Dim cRow = DataGridView1.Rows.Cast(Of DataGridViewRow)().FirstOrDefault(Function(r) CBool(r.Cells("ID").Value = i))  
                cRow .Cells("Col1").Value = i  

    In this example it works fine but in my real sample whatever the value i input manually or programmatically disappears immediately . any idea why this is happening?


  4. Hobbyist_programmer 621 Reputation points

    I think it is better to use datatable for this purpose as you said. thanks.

    0 comments No comments