C1FlexGrid: Custom column functionality using ColumnValueConverter

Posted by: cnickerson on 9 July 2025, 4:52 pm EST

  • Posted 9 July 2025, 4:52 pm EST - Updated 9 July 2025, 5:00 pm EST

    Hello,

    I have a C1FlexGrid in WPF 4.6.2 - 2024v1 (839). I am using ColumnValueConverters on some of my fields.

    <c1:C1FlexGrid
    	x:Name="flexGrid"
        	AutoGenerateColumns="False"
        	HorizontalAlignment="Stretch" 
        	CellEditEnded="flexGrid_CellEditEnded"
       	BeginningEdit="flexGrid_BeginningEdit"
        	>
    	<c1:C1FlexGrid.Columns>
    		<c1:Column Binding="{Binding fkProjectID}" Header="Project" ColumnName="colProject" />
    		<c1:Column Binding="{Binding fkSubProjectID}" Header="SubProject" ColumnName="colSubProject" />
    	</c1:C1FlexGrid.Columns>
    </c1:C1FlexGrid>
    Dim controller As New Controller
    
    flexGrid.ItemsSource = controller.GetObjectDetails() ' pkObjectDetailID, fkProjectID, fkSubProjectID
    
    Dim projectList = From row In controller.GetProjectList().AsEnumerable()
    		Select New With
    		{
    			.pkProjectID = row.Field(Of Integer)("pkProjectID"),
    			.ProjectName = row.Field(Of String)("ProjectName")
    		}
    
    Dim subProjectList = From row In controller.GetSubProjectList().AsEnumerable()
    		Select New With
    		{
    			.pkSubProjectID = row.Field(Of Integer)("pkSubProjectID"),
    			.ProjectName = row.Field(Of String)("ProjectName")
    		}                	
    
    flexGrid.Columns("colProject").ValueConverter = New ColumnValueConverter(projectList, "pkProjectID", "ProjectName")
    flexGrid.Columns("colSubProject").ValueConverter = New ColumnValueConverter(subProjectList, "pkSubProjectID", "ProjectName")

    I want the functionality of my drop-downs to be as follows:

    1. When I select a new value of Project, I want the Sub-Project to be set to blank to force the user to enter a new value there.

    2. I want the Combo Box to be restricted to Sub Projects of the selected Project, not all of them.

    So far, my attempts at a solution was to set the binded item to DBNull inside of a CellEditEnded event of my FlexGrid. I tried setting to NULL, but it throws an error that I “Cannot set Column ‘fkSubProjectID’ to be null. Please use DBNull instead.”

    Dim oOriginalValues As Object
    
    Private Sub flexGrid_BeginningEdit(sender As C1FlexGrid, e As CellEditEventArgs)
    	oOriginalValues = New With
    		{
    			.fkProjectID = IIf(IsDBNull(sender.Rows(e.Row).DataItem.Item("fkProjectID")), Nothing, sender.Rows(e.Row).DataItem.Item("fkProjectID")),
                		.fkSubProjectID = IIf(IsDBNull(sender.Rows(e.Row).DataItem.Item("fkSubProjectID")), Nothing, sender.Rows(e.Row).DataItem.Item("fkSubProjectID"))
    		}
    End Sub
    
    Private Sub flexGrid_CellEditEnded(sender As C1FlexGrid, e As CellEditEventArgs)
            With sender.Rows(e.Row).DataItem
                	If .Item("fkProjectID") <> oOriginalValues.fkProjectID Then
                    	.Item("fkSubProjectID") = DBNull.Value
    		End If
            End With
    End Sub

    However, because the value is DBNull instead of Null, whenever I select the blank cell and then click off without setting a value, the ColumnValueConverter class throws an exception saying “Value not found in converter dictionary.” and the application crashes.

    I have a few questions regarding my requirements:

    1. How would I access the C1FlexComboBox that gets created on the columns, if possible?

    2. How can I assign a value to my sender.Rows(e.Row).DataItem.Item(“fkSubProjectID”) and still use ColumnValueConverters?

  • Posted 9 July 2025, 4:57 pm EST - Updated 9 July 2025, 4:59 pm EST

    Edit: Disregard. Edited original post instead.

  • Posted 10 July 2025, 1:00 pm EST

    Hi Chris,

    Thanks for sharing the code snippet.

    >1. How would I access the C1FlexComboBox that gets created on the columns, if possible?

    You can fetch the C1FlexComboBox when the cell edit starts. For this, you have to handle FlexGrid’s PrepareCellForEdit event. Here is the code snippet to achieve the required behavior:

    Private Sub flexGrid_PrepareCellForEdit(sender As Object, e As CellEditEventArgs)
        Dim bdr = TryCast(TryCast(sender, C1FlexGrid).ActiveEditor, Border)
        If bdr IsNot Nothing Then
            Dim cmb = TryCast(bdr.Child, C1FlexComboBox)
            If cmb IsNot Nothing Then
                Debug.WriteLine("C1FlexComboBox object fetched")
                ' You can perform the operations here
            End If
        End If
    End Sub

    Kindly refer to the attached sample for full implementation. See ColValConv_FW.zip.

    2. How can I assign a value to my sender.Rows(e.Row).DataItem.Item(“fkSubProjectID”) and still use ColumnValueConverters?

    You can use the following code to set the required property of the data item:

    CType(flexGrid.Rows(e.Row).DataItem, ObjectDetail).fkProjectID = 2

    If the issue persists, we kindly request you update the attached sample and share a video/gif showing the issue occurring on your end. This will help us understand your use-case exactly to provide you with a more specific solution.

    Thanks & regards,

    Aastha

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels