Results 1 to 3 of 3

Thread: DataGridView DataGridViewComboBoxColumn not displaying datatable number values

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    55

    DataGridView DataGridViewComboBoxColumn not displaying datatable number values

    I dynamically create columns in my datagridview
    The columns that are ComboBox have items added to them when they are being created.
    Most of these items are strings, but one column has numbers added.
    I then bind the datagridview to a datatable.
    When the datagrid is displayed the comboboxes that have associated with strings display the datatable value
    the combobox that is associalted with numbers (singles) displays the first item in the list.

    Any suggestions?

    Code:
    Sub AddComboColumn(ThisView As DataGridView, ByVal MyList(,) As String, icol As Integer, sName As String, oType As Type, iWidth As Integer)
    		Dim cmbcol As DataGridViewComboBoxColumn
    		cmbcol = New DataGridViewComboBoxColumn()
    		cmbcol.HeaderText = sName
    		cmbcol.Name = sName
    		cmbcol.DataPropertyName = sName
    		cmbcol.DisplayMember = cmbcol.DataSource
    		cmbcol.ValueType = oType
    		cmbcol.ReadOnly = False
    		cmbcol.Width = iWidth
    
    		Dim ir As Integer
    		For ir = 0 To UBound(MyList, 2)  '   MyList.Count - 1
    
    			If MyList(icol, ir) = "" Then Exit For
    			If oType = GetType(Single) Then
    				cmbcol.Items.Add(Math.Round(Val(MyList(icol, ir)), 2))
    			Else
    				cmbcol.Items.Add(MyList(icol, ir))
    			End If
    		Next ir
    
    		ThisView.Columns.Add(cmbcol)
    
    	End Sub

    Code:
    Dim PBBindingSource As New BindingSource
    		Dim ThisDataView As New DataView
    		ThisDataView = New DataView(rs)
    		ThisDataView.RowFilter = ""
    		ThisDataView.Sort = ""
    
    		PBBindingSource.DataSource = ThisDataView.ToTable(True, msShowField)
    		PBBindingSource.Filter = ""
    		PBBindingSource.Sort = ""
    		dgConfiguration.DataSource = PBBindingSource
    		dgConfiguration.Refresh()

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,468

    Re: DataGridView DataGridViewComboBoxColumn not displaying datatable number values

    You are rounding the numbers before adding them to the combobox, which means that they probably wont match the values in the DataTable - so the first value gets displayed as the default.

    Instead of rounding the numbers, try formatting the DataGridView column (and/or combobox), eg:
    Code:
        dataGridView1.Columns("Column Name").DefaultCellStyle.Format = "N2"
    https://docs.microsoft.com/en-us/dot...format-strings

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    55

    Re: DataGridView DataGridViewComboBoxColumn not displaying datatable number values

    Quote Originally Posted by si_the_geek View Post
    You are rounding the numbers before adding them to the combobox, which means that they probably wont match the values in the DataTable - so the first value gets displayed as the default.

    Instead of rounding the numbers, try formatting the DataGridView column (and/or combobox), eg:
    Code:
        dataGridView1.Columns("Column Name").DefaultCellStyle.Format = "N2"
    https://docs.microsoft.com/en-us/dot...format-strings

    Many thanks, a great idea, But it did not make a difference. If I go to the immediate window I see that the cells value matches the value in the datatable.
    Is their a way, after the load event, to go through each cell, find the dropdown index that matches the table value and assign that to each of the offending cells?

    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width