Avatar of curiouswebster
curiouswebster
Flag for United States of America asked on

Making a generic list a DataSource for a DataGridView

I have a generic list and the DataGridView it is to populate. How do I do this?

I see I must set the DataSource to be the generic list. But how do I associate DataGridView with properties in the generic list? Or is there another way to connect them?

Thanks,
newbieweb
.NET ProgrammingC#

Avatar of undefined
Last Comment
curiouswebster

8/22/2022 - Mon
krunal_shah

curiouswebster

ASKER
I am using C# WinForms. But these articles are for ASP.NET.

It's be great if you had one I know WinForms supports.

Thanks,
newbieweb
strider_1981

Try this..

'''''''''''''''''''''''Form code'''''''''''''''''''''''
Imports System.Xml
Imports System.Collections.Generic

Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim intCounter As Int32 = 1
        Dim dtDate As DateTime = Date.Today
        Dim lstVisits As New List(Of MonthlyVisit)
        For intCounter = 1 To 12
            Dim objVisit As New MonthlyVisit
            objVisit.Month = dtDate.Month
            objVisit.MonthAndYear = MonthName(dtDate.Month, True) & " " & dtDate.Year.ToString()
            objVisit.Visits = 1000 + (intCounter * 12)
            objVisit.Year = dtDate.Year
            lstVisits.Add(objVisit)

            dtDate = DateAdd(DateInterval.Month, 1, dtDate)
        Next

        'SimpleBinding(lstVisits)
        CreateColumnAndBind(lstVisits)
    End Sub

    Private Sub SimpleBinding(ByVal Data As List(Of MonthlyVisit))
        DataGridView1.DataSource = Data
    End Sub

    Private Sub CreateColumnAndBind(ByVal Data As List(Of MonthlyVisit))
        Dim objColumn As DataGridViewColumn
        Dim objCellTemplate As DataGridViewCell

        ''Add Month and year Column
        objColumn = New DataGridViewColumn()
        objCellTemplate = New DataGridViewTextBoxCell
        objCellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
        objColumn.CellTemplate = objCellTemplate
        objColumn.DataPropertyName = "MonthAndYear"
        objColumn.HeaderText = "Month"
        objColumn.Width = 200
        DataGridView1.Columns.Add(objColumn)

        ''Add Visits Column
        objColumn = New DataGridViewColumn()
        objCellTemplate = New DataGridViewLinkCell
        objCellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        objColumn.CellTemplate = objCellTemplate
        objColumn.DataPropertyName = "Visits"
        objColumn.HeaderText = "Visits This Month"
        objColumn.Width = 200
        DataGridView1.Columns.Add(objColumn)

        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = Data
    End Sub
End Class

'''''''''''''''''''''''MonthlyVisit class'''''''''''''''''''''''

Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Xml.Serialization


Public Class MonthlyVisit
#Region "Private members"
    Private _intMonth As Int32
    Private _intYear As Int32
    Private _strMonthAndYear As String
    Private _intVisits As Int32
#End Region

#Region "Constructors"
    Public Sub New()

    End Sub
#End Region

#Region "Public properties"
    Public Property Month() As Int32
        Get
            Return _intMonth
        End Get
        Set(ByVal value As Int32)
            _intMonth = value
        End Set
    End Property

    Public Property Year() As Int32
        Get
            Return _intYear
        End Get
        Set(ByVal value As Int32)
            _intYear = value
        End Set
    End Property

    Public Property Visits() As Int32
        Get
            Return _intVisits
        End Get
        Set(ByVal value As Int32)
            _intVisits = value
        End Set
    End Property

    Public Property MonthAndYear() As String
        Get
            Return _strMonthAndYear
        End Get
        Set(ByVal value As String)
            _strMonthAndYear = value
        End Set
    End Property
#End Region
End Class

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
strider_1981

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
krunal_shah

curiouswebster

ASKER
Perfect!
curiouswebster

ASKER
strider_1981,

You post was very clear for me to code. BUT, my object has other properties and puts them all into the DataGridView. I read you post and improvisedc, since I'd rather lay out the grid using the designer. So I just added this code:

            driversDataGridView.Columns[0].DataPropertyName = "DriverLabel";
            driversDataGridView.Columns[1].DataPropertyName = "Status";
            driversDataGridView.Columns[2].DataPropertyName = "Phone";
            driversDataGridView.Columns[3].DataPropertyName = "GPS";


since the columns already existed.

How can I force the DataGridView to not add any new columns?

Thanks,
newbieweb
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
curiouswebster

ASKER