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

ThreeState Checkbox

2 Answers 184 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 17 Apr 2016, 06:27 PM

Hi,

 

I have a dataGrid with a threestate Checkbox-Column. Off = 0, On = 1, Indeterminate = 2.

State Off and On ok, but Indeterminate only the last is show correctly.

After fill the dataGrid On and Indeterminate always the same. I don't know what is wrong.

 

Database Columns are:

Code varchar(10)
Description varchar(50)
Sort tinyint  (0=off,1=on,2=indeterminate)

 

Fill datagrid:

private bool LoadStreets()
        {
            bool bOk = true;
            try
            {
                context.Street.Load();
                bindingSourceStreet.DataSource = context.Strassen.Local.ToBindingList();
            }
            catch (DbEntityValidationException ex)
            {
                bOk = false;
            }

            return bOk;
        }

Please can you tell me what is wrong?

Thank you

Andreas

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Apr 2016, 08:06 AM
Hello Andreas,

Thank you for writing.

The GridViewDataColumn provides a unified way of converting incompatible value types to a type supported by the column instance. It is suitable to use a custom TypeConverter to convert the value to ToggleState. Additional information is available here: http://docs.telerik.com/devtools/winforms/gridview/columns/converting-data-types

The following code snippet demonstrates a approach as well:
public Form1()
{
    InitializeComponent();
    GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn("Sort");
    checkBoxColumn.ThreeState = true;
    checkBoxColumn.DataType = typeof(int);
    checkBoxColumn.FieldName = "Sort";
    checkBoxColumn.DataTypeConverter = new ToggleStateConverter();
    radGridView1.MasterTemplate.Columns.Add(checkBoxColumn);
}
 
public class ToggleStateConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(ToggleState);
    }
 
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        byte byteValue = (byte)value;
 
        switch (byteValue)
        {
            case 1:
                return ToggleState.On;
            case 0:
                return ToggleState.Off;
            case 2:
                return ToggleState.Indeterminate;
        }
 
        return base.ConvertTo(context, culture, value, destinationType);
    }
 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(ToggleState);
    }
 
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        ToggleState state = (ToggleState)value;
 
        switch (state)
        {
            case ToggleState.On:
                return 1;
            case ToggleState.Off:
                return 0;
            case ToggleState.Indeterminate:
                return 2;
        }
        return base.ConvertFrom(context, culture, value);
    }
}

I hope this information helps. Should you have further questions I would be glad to help.

 Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Andreas
Top achievements
Rank 1
answered on 20 Apr 2016, 09:34 AM

Hi Dess,

 

thank you merry much, it's work perfectly.

Regards,

Andreas

Tags
GridView
Asked by
Andreas
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Andreas
Top achievements
Rank 1
Share this question
or