1

Hi I know this may sound a little silly but I want to use a DataGridViewCheckBoxColumn on a field in a database that either contains 'Y', 'N', or blank values. I want the Y values to be checked and everything else to be unchecked.

I am using VB.NET by the way and any help with this would be appreciated. Also before anybody asks no I can't change the database field to boolean I have to just roll with it the way it is.

slister
  • 769
  • 1
  • 13
  • 29

3 Answers3

1

You can use a IValueConverter, something like this:

 public class YesNoToBooleanConverter : IValueConverter
 {

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {

        return ((value as string).Equals("Y")) ? true : false;
     }

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
        return value;
     }
 }

In binding using converter, If you are using wpf, this how you bind it.

this just an example:

  1. xmlns:local="clr-namespace:project.converter"

Under resources:

  1. <Grid.Resources> <local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" /> </Grid.Resources>

In your column

  1. DataMemberBinding="{Binding Active, Converter={StaticResource YesNoToBooleanConverter}}"
0

Try like this

get the output of query is boolean like this

select case when column1="Y" then true else false end as column from table1

after that assign that data source in datagridview

Sathish
  • 4,419
  • 4
  • 30
  • 59
0

As I said in a comment when answering one of your previous questions: I'm pretty sure I've done that before. Well guess what, I had, and the solution is pretty simple.

The DataGridViewCheckBoxColumn class has two properties to help you.

  • FalseValue: Gets or sets the underlying value corresponding to a cell value of true, which appears as a checked box.

  • TrueValue: Gets or sets the underlying value corresponding to a cell value of false, which appears as an unchecked box.

With Me.MyDataGridViewCheckBoxColumnInstance
    .FalseValue = "N"
    .TrueValue = "Y"
    .DataPropertyName = "MyYesNoDataColumnName"
End With
Community
  • 1
  • 1
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64