1 Replies - 195 Views - Last Post: 15 April 2019 - 12:52 AM Rate Topic: -----

#1 nyt1972   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 100
  • Joined: 04-February 10

[Resolved] Fill Datagridview programmatically

Posted 14 April 2019 - 09:55 PM

Dear Sir,

I am working on a student Attendance System, and for that I want to add a Column from datatable as Student Names, and other Columns as Days of Months.

So for I tried below, but it duplicates rows and columns, image is attached.

If dt.Rows.Count > 0 Then
                Dim data As New List(Of String)
                For Each row As DataRow In dt.Rows
                    data.Add(row.Item("StudentName"))
                Next
                '  MsgBox(data.Item(1))
                For Each dataItem As String In data
                    Dim col1 As New DataGridViewTextBoxColumn
                    col1.HeaderText = "Student"
                    dgw.Columns.Add(col1)
                    dgw.Rows.Add(dataItem)
                    For i As Integer = 0 To 4
                        Dim col2 As New DataGridViewCheckBoxColumn
                        col2.HeaderText = "col_header"
                        dgw.Columns.Add(col2)
                    Next
                Next
            End If



Pls help.

Attached image(s)

  • Attached Image

This post has been edited by nyt1972: 15 April 2019 - 12:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Resolved] Fill Datagridview programmatically

#2 nyt1972   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 100
  • Joined: 04-February 10

Re: [Resolved] Fill Datagridview programmatically

Posted 15 April 2019 - 12:52 AM

Thanks gurus,

I tried and tried and finally fixed the problem with following code:

 
' dt is DataTable
If dt.Rows.Count > 0 Then
                Dim data As New List(Of String)
                For Each row As DataRow In dt.Rows
                    data.Add(row.Item("StudentName"))
                Next
                Dim col1 As New DataGridViewTextBoxColumn
                col1.HeaderText = "Student"
                col1.Name = "StudentName"
                col1.DataPropertyName = "StudentName"
                dgw.Columns.Add(col1)
                '   dgw.Columns.Add("StudentName", "Student")
                dgw.DataSource = dt
                For i As Integer = 0 To 4
                        Dim col2 As New DataGridViewCheckBoxColumn
                        col2.HeaderText = "col_header"
                        dgw.Columns.Add(col2)
                    Next
                '     Next
            End If

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1