curiouswebster
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
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect!
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.Column s[0].DataP ropertyNam e = "DriverLabel";
driversDataGridView.Column s[1].DataP ropertyNam e = "Status";
driversDataGridView.Column s[2].DataP ropertyNam e = "Phone";
driversDataGridView.Column s[3].DataP ropertyNam e = "GPS";
since the columns already existed.
How can I force the DataGridView to not add any new columns?
Thanks,
newbieweb
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.Column
driversDataGridView.Column
driversDataGridView.Column
driversDataGridView.Column
since the columns already existed.
How can I force the DataGridView to not add any new columns?
Thanks,
newbieweb
ASKER
If you have an answer, please post it here:
https://www.experts-exchange.com/questions/25151196/How-do-I-block-the-DataGridView-from-forcing-any-new-columns-from-the-generic-list.html?fromWizard=true
https://www.experts-exchange.com/questions/25151196/How-do-I-block-the-DataGridView-from-forcing-any-new-columns-from-the-generic-list.html?fromWizard=true
http://forums.asp.net/t/1311737.aspx
http://www.codegod.de/WebAppCodeGod/webservice-aspnet-with-gridview-and-datagridview-AID310.aspx
http://www.eggheadcafe.com/software/aspnet/32061094/binding-a-generic-list-to.aspx