This is a migrated thread and some comments may be shown as answers.

GridViewComboBoxColumn does not display value until clicked

2 Answers 128 Views
This is a migrated thread and some comments may be shown as answers.
Ali Mohamad
Top achievements
Rank 1
Ali Mohamad asked on 18 Mar 2011, 08:03 AM
I have a simple databound grid (grdPermissions) that has one "tinyint" column with possible values of 0, 1, or 2, which stand for Deny, Grant, or Not Set correspondingly.  This column is being added as a GridViewComboBoxColumn and bound to a List as shown in the code below.  The problem is that when the grid is bound and the data displayed, the combobox does not display its data until clicked with the mouse.  The combobox seems to have the correct value set, but shows nothing until clicked.  Also, once the combobox is closed the cell returns to showing nothing.  You can see the behaviour in the attached images; image1 shows the initial display of the grid, image2 shows how the cell displays the value when clicked, image3 shows the cell in another row displays its value but the previously selected combobox in the row above now displays no data.
What might I be doing wrong?

'**** BEGIN CODE ****
Imports Telerik.WinControls.UI
Imports System.Data
Imports System.Data.SqlClient

Public Class frmGroupPerms
    Private Sub somefunction()
        '** the GetGroupPermissions() function retrieves a table with the
        '** following columns
        '**    GroupID  int,
        '**    Name  nvarchar(255),
        '**    Description  nvarchar(255),
        '**    AppPermissionType  nvarchar(30),
        '**    PermValue  tinyint - can be 0, 1, or 2
        Dim _groupPermissions As DataTable = GetGroupPermissions(_groupId)
        grdPermissions.DataSource = _groupPermissions
    End Sub

    Private Sub _ConfigureGrid()
        Dim _perms As List(Of ComboItem) = New List(Of ComboItem)
        _perms.Add(New ComboItem(0, "Deny"))
        _perms.Add(New ComboItem(1, "Grant"))
        _perms.Add(New ComboItem(2, "Not Set"))

        Me.grdPermissions.AutoGenerateColumns = False
        Me.grdPermissions.Columns.Add(New GridViewTextBoxColumn("GroupId"))
        Me.grdPermissions.Columns.Add(New GridViewTextBoxColumn("Name"))
        Me.grdPermissions.Columns.Add(New GridViewTextBoxColumn("Description"))
        Me.grdPermissions.Columns.Add(New GridViewTextBoxColumn("AppPermissionType"))
        Dim _cmb As New GridViewComboBoxColumn()
        _cmb.Width = 150
        _cmb.Name = "PermValue"
        _cmb.FieldName = "PermValue"
        _cmb.HeaderText = "Permission"
        _cmb.DataSource = _perms
        _cmb.ValueMember = "ItemValue"
        _cmb.DisplayMember = "ItemText"
    End Sub

    Private Sub grdPermissions_DataBindingComplete(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewBindingCompleteEventArgs) Handles grdPermissions.DataBindingComplete
        grdPermissions.MasterTemplate.Columns("GroupId").IsVisible = False
        grdPermissions.MasterTemplate.Columns("Name").Width = 150
        grdPermissions.MasterTemplate.Columns("Description").Width = 150
        grdPermissions.MasterTemplate.Columns("AppPermissionType").Width = 200
    End Sub
End Class

Friend Class ComboItem
    Public Property ItemText As String
    Public Property ItemValue() As Short

    ''' <summary>
    ''' Initializes a new instance of the ComboItem class.
    ''' </summary>
    ''' <param name="itemValue"></param>
    ''' <param name="itemText"></param>
    Public Sub New(ByVal itemValue As Short, ByVal itemText As String)
        Me.ItemValue = itemValue
        Me.ItemText = itemText
    End Sub
End Class
'**** END CODE ****

2 Answers, 1 is accepted

Sort by
Richard Slade
Top achievements
Rank 2
answered on 18 Mar 2011, 12:17 PM

Ensure that you're combobox column has a valid dislaymemeber and valuemember. I have included below a full exmaple that you can paste into a new project.

Designer File
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    'Form overrides dispose to clean up the component list.   
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        End Try
    End Sub
    'Required by the Windows Form Designer   
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer   
    'It can be modified using the Windows Form Designer.     
    'Do not modify it using the code editor.   
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadGridView1 = New Telerik.WinControls.UI.RadGridView()
        CType(Me.RadGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.RadGridView1.BackColor = System.Drawing.SystemColors.Control
        Me.RadGridView1.Cursor = System.Windows.Forms.Cursors.Default
        Me.RadGridView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.RadGridView1.Font = New System.Drawing.Font("Segoe UI", 8.25!)
        Me.RadGridView1.ForeColor = System.Drawing.SystemColors.ControlText
        Me.RadGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.RadGridView1.Location = New System.Drawing.Point(0, 0)
        Me.RadGridView1.MasterTemplate.AllowAddNewRow = False
        Me.RadGridView1.MasterTemplate.AllowDeleteRow = False
        Me.RadGridView1.MasterTemplate.AllowEditRow = False
        Me.RadGridView1.MasterTemplate.AutoGenerateColumns = False
        Me.RadGridView1.MasterTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill
        Me.RadGridView1.MasterTemplate.EnableAlternatingRowColor = True
        Me.RadGridView1.MasterTemplate.ShowRowHeaderColumn = False
        Me.RadGridView1.Name = "RadGridView1"
        Me.RadGridView1.Padding = New System.Windows.Forms.Padding(0, 0, 0, 1)
        Me.RadGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.RadGridView1.RootElement.Padding = New System.Windows.Forms.Padding(0, 0, 0, 1)
        Me.RadGridView1.ShowGroupPanel = False
        Me.RadGridView1.Size = New System.Drawing.Size(367, 228)
        Me.RadGridView1.TabIndex = 0
        Me.RadGridView1.Text = "RadGridView1"
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ClientSize = New System.Drawing.Size(367, 228)
        Me.Name = "Form1"
        Me.Text = "Form 1"
        CType(Me.RadGridView1, System.ComponentModel.ISupportInitialize).EndInit()
    End Sub
    Friend WithEvents RadGridView1 As Telerik.WinControls.UI.RadGridView
End Class

Imports Telerik.WinControls.UI
Imports System.Text
Imports System.ComponentModel
Imports Telerik.WinControls
Imports Telerik.WinControls.Data
Imports Telerik.WinControls.Primitives
Public Class Form1
    Private m_List As New List(Of MyObject)
    Private m_Permissions As New List(Of Permission)
    Public Sub New()
        RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
        RadGridView1.AutoGenerateColumns = False
        RadGridView1.AllowEditRow = True
        Dim nameColumn As New GridViewTextBoxColumn()
        nameColumn.HeaderText = "Name"
        nameColumn.Name = "Name"
        nameColumn.FieldName = "Name"
        Dim descriptionColumn As New GridViewTextBoxColumn()
        descriptionColumn.HeaderText = "Description"
        descriptionColumn.Name = "Description"
        descriptionColumn.FieldName = "Description"
        Dim permissionColumn As New GridViewComboBoxColumn()
        permissionColumn.HeaderText = "Permission"
        permissionColumn.Name = "Permission"
        permissionColumn.FieldName = "Permission"
        permissionColumn.DataSource = m_Permissions
        permissionColumn.DisplayMember = "Name"
        permissionColumn.ValueMember = "Level"
        Me.RadGridView1.DataSource = m_List
    End Sub
    Private Sub Populate()
        m_Permissions.Add(New Permission(1, "Low"))
        m_Permissions.Add(New Permission(2, "Medium"))
        m_Permissions.Add(New Permission(3, "High"))
        m_Permissions.Add(New Permission(4, "Very High"))
        m_List.Add(New MyObject("Name 1", "Description 1", 1))
        m_List.Add(New MyObject("Name 2", "Description 2", 2))
        m_List.Add(New MyObject("Name 3", "Description 3", 3))
        m_List.Add(New MyObject("Name 4", "Description 4", 4))
    End Sub
End Class
Public Class MyObject
    Public Sub New()
    End Sub
    Public Sub New(ByVal name As String, ByVal description As String, ByVal permission As Integer)
        Me.Name = name
        Me.Description = description
        Me.Permission = permission
    End Sub
    Public Property Name As String
    Public Property Description As String
    Public Property Permission As Integer
End Class
Public Class Permission
    Public Sub New(ByVal level As Integer, ByVal name As String)
        Me.Name = name
        Me.Level = level
    End Sub
    Public Property Name As String
    Public Property Level As Integer
End Class

Hope that helps but let me know if you have any questions
Telerik team
answered on 23 Mar 2011, 02:50 PM
Hi Ali Mohamad,

Thank you for writing.

The observed behavior is caused by types conflict. Your data source returns the value for the PermColumn as Byte and the property in your custom class is Short. Please change the ItemValue property type to Byte and everything should be fine. 

If you continue experiencing this issue, do not hesitate to write back.
the Telerik team
Asked by
Ali Mohamad
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Telerik team
Share this question