[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

list all fields/coloumns in bindingsource and add them to unbound datagridview vb.net

Hi I want to list all fields/coloumns in bindingsource and add them to unbound datagridview.
I have a unbound datagridview and set out how I want it to look. I quite new to programing so please show in detail.

Iam using
1 Solution
Coralie CrumrineCommented:
What I did was create a function to format the datagridview column:

Public Function SetDViewTextBoxColumn(ByRef objSrc As DataGridViewTextBoxColumn, ByVal szMappingName As String, ByVal szHeaderText As String, ByVal lWidth As Int32, Optional ByVal lAutoSizeMode As DataGridViewAutoSizeColumnMode = DataGridViewAutoSizeColumnMode.Fill, Optional ByVal szFormat As String = "", Optional ByVal lAlignment As DataGridViewContentAlignment = DataGridViewContentAlignment.MiddleCenter, Optional ByVal szNullText As String = "", Optional ByVal blIsVisible As Boolean = True, Optional ByVal blIsReadonly As Boolean = False) As DataGridViewTextBoxColumn
        With objSrc
            .Name = szMappingName
            .DataPropertyName = szMappingName

            If szHeaderText <> "" Then
                .HeaderText = szHeaderText
                .HeaderText = szMappingName
            End If

            If szFormat <> "" Then
                .DefaultCellStyle.Format = szFormat
            End If

            .DefaultCellStyle.Alignment = lAlignment
            .DefaultCellStyle.NullValue = szNullText
            .Width = lWidth
            .Visible = blIsVisible
            .AutoSizeMode = lAutoSizeMode
            .ReadOnly = blIsReadonly
        End With

        Return objSrc
        'If Not objTableStyle Is Nothing Then objTableStyle.GridColumnStyles.Add(objSrc)
    End Function

then assign which columns out of the table I want to show


            dgvView.AutoGenerateColumns = False
            dgvView.DataSource = tbl

            Dim dvColDateVisit As New DataGridViewTextBoxColumn
            dgvView.Columns.Add(SetDViewTextBoxColumn(dvColDateVisit, "DateVisit", "Date Visit", 75, , "d", DataGridViewContentAlignment.MiddleRight))

I hope that this helps. Let me know if you have any questions or this does not work for you.
What is the datasource of the binding source? Is it a datatable?

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now