Welcome to the Xceed Community Sign in | Join | Help
Community Search  

Question about checkboxes and databases that don't have bools yet

Sort Posts: Previous Next
  •  04-25-2008, 3:28 PM Post no. 11756

    Question about checkboxes and databases that don't have bools yet

    Hello,

    I am using a MySQL database....and for some reason they still don't support booleans.  No  (But they promise they will in the future)  Yes 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)

     Beer

     

  •  04-25-2008, 4:02 PM Post no. 11759 in reply to 11756

    Re: Question about checkboxes and databases that don't have bools yet

    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>


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
    Filed under:
  •  04-28-2008, 9:30 AM Post no. 11785 in reply to 11759

    Re: Question about checkboxes and databases that don't have bools yet

    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.

     

  •  04-28-2008, 9:40 AM Post no. 11787 in reply to 11785

    Re: Question about checkboxes and databases that don't have bools yet

    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>


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  04-28-2008, 10:47 AM Post no. 11798 in reply to 11787

    Re: Question about checkboxes and databases that don't have bools yet

    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>

  •  04-28-2008, 11:09 AM Post no. 11800 in reply to 11798

    Re: Question about checkboxes and databases that don't have bools yet

    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!


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-21-2008, 5:31 AM Post no. 12396 in reply to 11800

    Re: Question about checkboxes and databases that don't have bools yet

    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 


     

  •  05-21-2008, 7:43 AM Post no. 12399 in reply to 12396

    Re: Question about checkboxes and databases that don't have bools yet

    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);

       }

    }

  •  07-23-2008, 3:09 AM Post no. 13699 in reply to 12399

    Re: Question about checkboxes and databases that don't have bools yet

    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.

     

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.