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

Checkbox isChecked binding

2 Answers 1600 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Start Informatica
Top achievements
Rank 1
Start Informatica asked on 29 Aug 2016, 03:48 PM

Hi, I have a RadGridView with a column having a checkbox inside that I should use to activate or deactivate the user. I retrieve the rows of my radgridview from a query in sqlserver. The problem is with the checkbox column: I can't get the checkbox checked based on the value in my sql column (bit). This is how I retrive the rows:

Dim dt As New DataTable
 
        Using sqlCon = New SqlConnection(ConnectionStringRemote)
            sqlCon.Open()
 
            Dim cmd = New SqlCommand("", sqlCon)
            cmd.CommandText = "SELECT        MA_Carriers.Carrier, MA_Carriers.CompanyName, ISNULL(STRT_CarriersManager.Activated,0) AS Attivo
FROM            STRT_CarriersManager RIGHT OUTER JOIN
MA_Carriers ON STRT_CarriersManager.Carrier = MA_Carriers.Carrier WHERE MA_Carriers.Disabled=0"
            Dim sda = New SqlDataAdapter(cmd)
 
            sda.Fill(dt)
            gridCarriers.ItemsSource = dt
 
        End Using

 

And this is the XAML:

<UserControl x:Class="CarriersManager"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Dalessio_Logistics"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d"
             d:DesignHeight="500" d:DesignWidth="1100" Background="White">
     
    <UserControl.Resources>
        <local:BoolConverter x:Key="BoolConverter" />
    </UserControl.Resources>
 
    <StackPanel Margin="0,0,0,0" Height="auto" HorizontalAlignment="Stretch">
            <StackPanel x:Name="panelTitle" Height="50" VerticalAlignment="Top" DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Stretch">
                <telerik:Label Content="Gestione operatori:"/>
            </StackPanel>
            <StackPanel x:Name="spContent">
            <telerik:RadGridView x:Name="gridCarriers"  AutoGenerateColumns="False" CanUserInsertRows="False" CanUserDeleteRows="False" RowIndicatorVisibility="Collapsed"
            ShowGroupPanel="False" IsReadOnly="False" Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" SelectionMode="Extended" SelectionUnit="FullRow"
            CanUserReorderColumns="False" FontFamily="Source Sans Pro Semibold" FontSize="14" GridLinesVisibility="Horizontal" IsFilteringAllowed="True" ShowSearchPanel="True" RowHeight="45">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn x:Name="Carrier" UniqueName="Carrier" Header="Carrier" IsReadOnly="True" Width="150" DataMemberBinding="{Binding Carrier}"/>
                    <telerik:GridViewDataColumn x:Name="CompanyName" Header="CompanyName" IsReadOnly="True" DataMemberBinding="{Binding CompanyName}" Width="*"/>
                    <telerik:GridViewColumn Header="Attivo">
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=Attivo, Converter={StaticResource BoolConverter}}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </StackPanel>
        </StackPanel>
</UserControl>

And this is the BoolConverter : IValueConverter 

Public Class BoolConverter
    Implements IValueConverter
#Region "IValueConverter Members"
    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        If targetType <> GetType(Boolean) Then
            Throw New InvalidOperationException("The target must be a boolean")
        End If
 
        Return CBool(value)
    End Function
 
    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotSupportedException()
    End Function
#End Region
End Class

 

It doesn't work, my CheckBoxes are always unchecked. Can sameone help me please?

 

Thank you

 

 

 

2 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 31 Aug 2016, 10:15 AM
Hello,

Based on the code provided, I assume the issue you've described comes from the BoolConverter that has been set up. If the type of the Attivo property is Integer (0 and 1), the result of the following check should always be false and an exception should be thrown:

If targetType <> GetType(Boolean)

Instead, could you try replacing modifying the Convert method of your converter to the following:

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Dim isChecked = Integer.Parse(value.ToString()) = 1
 
    Return isChecked
End Function

On a side note, please bear in mind that when a DataTable is needed as a source collection of RadGridView, it is recommended to use its DefaultView property as specified in the following article.

gridCarriers.ItemsSource = dt.DefaultView

I'm attaching a sample project to demonstrate how I've set up the BoolConverter I mentioned previously. Could you please try incorporating this in your project and let me know if this fixes the issue? If that does not happen, please provide more details on the exact setup at your end and I will gladly assist you further.

Regards,
Dilyan Traykov
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Start Informatica
Top achievements
Rank 1
answered on 01 Sep 2016, 12:22 PM

As I wrote to the other thread this was the key: dt.DefaultView.

Before the function Convert wasn't even fired, now it is fired corectly so I was able to fix it in this way:

Public Class BoolConverter
    Implements IValueConverter
#Region "IValueConverter Members"
    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Return IIf(value.ToString = "True", True, False)
    End Function
 
    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Return IIf(value.ToString = "True", True, False)
    End Function
#End Region
End Class

Now everything works!

 

Thank you again Dilyan.

Tags
General Discussions
Asked by
Start Informatica
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Start Informatica
Top achievements
Rank 1
Share this question
or