0

I have taken checkbox in datagrid

<DataTemplate>
  <CheckBox x:Name="chkActive" IsChecked="{Binding Active, Mode=TwoWay}" 
    Style="{StaticResource checkboxStyleNormal}" IsEnabled="True"/>
 </DataTemplate>

and binding the datagrid

 datagrid1.ItemSource = dtData.DefaultView;

In datatable I am getting Active as 0 and 1, I want to show my check box checked when Active = 0

Grid is binding but I am unable to bind checkbox.

Some one please tell me how to show checkbox check/uncheck based on my condition.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Rocky
  • 4,454
  • 14
  • 64
  • 119
  • Have you set the context for your view? Have you implemented the `INotifiyPropertyChanged` interface on your object? Is `Active` a boolean type? – ChrisF Dec 28 '12 at 11:35
  • I'm assuming you meant "_getting Active as zero and one_" so I edited that line too. – gideon Dec 28 '12 at 11:36
  • @ChrisF: I am not using INotifiyPropertyChanged interface – Rocky Dec 28 '12 at 11:39

3 Answers3

5

IsChecked expects a boolean value (true/false) but the table contains a numeric type. You need to add a ValueConverter to the binding statement that will convert the numeric value to a boolean value.

Check How to bind a boolean to a combobox in WPF for the inverse case (convert an bool to an int). In your case, the ValueConverter should be:

public class NumToBoolConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((int)value == 1);   
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value ? 1 : 0;
    }
}

}

UPDATE

This post has a NumToBoolConverter that also does type and null checking:

public class NumToBoolConverter : IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
    {
        if (value!=null && value is int )
        {
            var val = (int)value;
            return (val==0) ? false : true;
        }
        return null;

    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
    {
        if (value!=null && value is bool )
        {
            var val = (bool)value;
            return val ? 1 : 0;
        }
        return null;
    }

    #endregion
}
Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • I am using UserControl for my Design. how can I use windows.resource – Rocky Dec 28 '12 at 12:03
  • Replace UserControl.Resource for Windows.Resource. Resource is a parameter of its parent object so you need to specify the parent's type, just as you do with DataGrid.XYZ – Panagiotis Kanavos Dec 28 '12 at 12:14
0

Try this if it helps:

  • Create a column derived from DataGridBoolColumn
  • Override the GetColumnValueAtRow (and SetColumnValueAtRow if you want your column to be editable)
  • Make these methods call the inherited implementations to work with the datasource, but do some pre-/post-processing to convert 0 and 1 to "false" and "true" respectively.
mkj
  • 2,761
  • 5
  • 24
  • 28
Sumit Pannu
  • 177
  • 1
  • 15
0

I tried by modify my query before binding to the datagrid and added "case" to my query

Select ID ,DESC, CASE WHEN [STATUS] = 0 THEN 'True' WHEN [STATUS] = 1 THEN 'False' END AS  [Active], ORDER [Order] from tbldesc

its working fine for me.

Rocky
  • 4,454
  • 14
  • 64
  • 119