Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag 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
Avatar of krunal_shah
krunal_shah

Avatar of 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
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

ASKER CERTIFIED SOLUTION
Avatar of strider_1981
strider_1981
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect!
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