Home Forums WPF controls Xceed DataGrid for WPF Question about checkboxes and databases that don’t have bools yet

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26954 |

    Hello,

    I am using a MySQL database….and for some reason they still don’t support booleans.  [N]  (But they promise they will in the future)  [Y] When I read my table in C# sees these fields as a short type.  Is there anyway to easily force the DataGrid to use checkboxes to represent these values?  (They will always be 1’s and 0’s)

     [B]

     

    Imported from legacy forums. Posted by Dave (had 1896 views)

    Xceed Support
    Member
    Post count: 5658

    You can change the column’s CellContentTemplate to a DataTemplate as demonstrates in the Providing a cell-content template example in the documentation:

    <Grid xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid“>
      <Grid.Resources>
        <xcdg:DataGridCollectionViewSource x:Key=”cvs_products”
                                        Source=”{Binding Source={x:Static Application.Current},
                                                          Path=Products}”/>
      </Grid.Resources>
       <xcdg:DataGridControl x:Name=”ProductsGrid”
                             ItemsSource=”{Binding Source={StaticResource cvs_products}}”>
          <xcdg:DataGridControl.Columns>

            <xcdg:Column FieldName=”Discontinued”>
               <xcdg:Column.CellContentTemplate>
                  <DataTemplate>
                     <Image x:Name=”img” Source=”D:\true.png” Stretch=”None” />
                        <DataTemplate.Triggers>
                           <DataTrigger Binding=”{Binding}” Value=”False”>
                             <Setter TargetName=”img” Property=”Source” Value=”D:\false.png” />
                           </DataTrigger>
                        </DataTemplate.Triggers>
                  </DataTemplate>
               </xcdg:Column.CellContentTemplate>
            </xcdg:Column>
          </xcdg:DataGridControl.Columns>
       </xcdg:DataGridControl>
    </Grid>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1650 views)

    User (Old forums)
    Member
    Post count: 23064

    I gave this a try…

    But it doesn’t act like a checkbox.  You can’t just click on that column and use it like a checkbox.  When you focus on it the graphic vanishes and you see either a 1 or a 0 and you need to manually type on a 1 or a 0.

     

    Imported from legacy forums. Posted by Dave (had 678 views)

    Xceed Support
    Member
    Post count: 5658

    You will also need to change the CellEditor if you are going to edit the data. The same kind of DataTemplate can be used. just make sure that you bind the IsChecked property of the CheckBox using a CellEditorBinding extension. For example:

    <DataTemplate>
      <CheckBox IsChecked=“{xcdg:CellEditorBinding}”/>
    </DataTemplate>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1827 views)

    User (Old forums)
    Member
    Post count: 23064

    Well I did that….I can see the Checkboxes now.  But when I try to check or uncheck one of those checkboxes it turns back into a text editor and I see the 0 or 1.  How can I force it to remain a checkbox?

     (Also just curious….the checkboxes look different than the ones you place on a WPF form (the ones you put on a the form have a green check when its true and the ones in the grid have a X when checked….just wondering why?)

     

    <xcdg:DataGridControl.Columns>

      <xcdg:Column FieldName=”Test1″>

        <xcdg:Column.CellContentTemplate>

          <DataTemplate>

            <CheckBox x:Name=”CheckBox” IsChecked=”{xcdg:CellEditorBinding}”/>

            <DataTemplate.Triggers>

              <DataTrigger Binding=”{Binding}” Value=”0″>

                <Setter TargetName=”CheckBox” Property=”IsChecked” Value=”False”/>

             </DataTrigger>

           <DataTrigger Binding=”{Binding}” Value=”1″>

             <Setter TargetName=”CheckBox” Property=”IsChecked” Value=”True”/>

          </DataTrigger>

        </DataTemplate.Triggers>

      </DataTemplate>

     </xcdg:Column.CellContentTemplate>

    </xcdg:Column>

    </xcdg:DataGridControl.Columns>

    Imported from legacy forums. Posted by Dave (had 876 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Dave,

    I should have been more specific. You need to change both the CellContentTemplate and the CellEditor in order to display a checkbox when the cell is being edited and when it is not.

    The CellEditorBinding markup extension can only be used within the context of a cell editor, it will do nothing when used in the CellContentTemplate. That said, try something like this:

         <xcdg:DataGridControl x:Name=”OrdersGrid”
                                ItemsSource=”{Binding Source={StaticResource cvs_product}}”>
             <xcdg:DataGridControl.Columns>
                <xcdg:Column FieldName=”Discontinued”>
                   <xcdg:Column.CellContentTemplate>
                      <DataTemplate>
                         <xcdg:CheckBox IsChecked=”{Binding Converter={StaticResource intToBooleanConverter}}” />
                      </DataTemplate>
                   </xcdg:Column.CellContentTemplate>
                   <xcdg:Column.CellEditor>
                      <xcdg:CellEditor>
                         <xcdg:CellEditor.EditTemplate>
                            <DataTemplate>
                               <xcdg:CheckBox IsChecked=”{xcdg:CellEditorBinding Converter={StaticResource intToBooleanConverter}}” />
                            </DataTemplate>
                         </xcdg:CellEditor.EditTemplate>
                      </xcdg:CellEditor>
                   </xcdg:Column.CellEditor>
                </xcdg:Column>
             </xcdg:DataGridControl.Columns>
          </xcdg:DataGridControl>

     The intToBooleanConverter static resource used a a converter simply converts your 0 or 1 value to a boolean and back. In your case, I recommend using a converter rather than DataTriggers. Like such:

    // declared in the resources of your main window

    <

    local:IntToBooleanConverter x:Key=”intToBooleanConverter” />

      public class IntToBooleanConverter: IValueConverter
      {
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
          if( value is int && ( int )value == 1 )
            return true;

          return false;
        }

        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
          if( value is bool && ( bool )value == true )
            return 1;

          return 0;
        }
      }

     Hope this clears things up!

    Imported from legacy forums. Posted by Jenny [Xceed] (had 2820 views)

    User (Old forums)
    Member
    Post count: 23064

    Jenny

    I had the same problem Dave had, but with your tips, I got it runnung … until I came to the point where the data should be written.

     
    I have a binding to a datatable and I do the update using

     

       Public Sub OnSave(ByVal sender As System.Object, ByVal e As RoutedEventArgs)

          Call Me.DataAdapterObj.Update(Me.DataTableObj)

       End Sub

    This works for all columns except for the booleans (which are, in my case, DECIMAL(1,0) on SQL server)

    When I debug and visualize the datatable I find that the boolean columns are not changed after checking/unchecking them, the other columns are changed !!!

    Do you have any hint?

    Regards

    Klaus 

     

    Imported from legacy forums. Posted by klaus (had 553 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Klaus,

     I did modify her code a bit…I am not sure if this will help you or not….but here it is:

     I believe I had to specifically cast the 1s and the 0s back into shorts….I would try that.

    public class ShortToBoolean_Converter: IValueConverter

    {

       public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )

       {

          if( value is short && ( short )value == 1 )

             return true;

          return false;

       }

       public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )

       {

          if( value is bool && ( bool )value == true )

             return ((short)1);

          return ((short)0);

       }

    }

    Imported from legacy forums. Posted by Dave (had 757 views)

    User (Old forums)
    Member
    Post count: 23064

    I have a datagrid control and there columns ID,Name, Description which arein database, grid is bound to database. my problem is i want to add new column in grid which displays checkboxes in column, but this column is not present in database. Is it possible or not.

     

    Imported from legacy forums. Posted by HSINGH (had 1896 views)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.