ThreeState Checkbox

3 posts, 0 answers
  1. Andreas
    Andreas avatar
    6 posts
    Member since:
    Feb 2016

    Posted 17 Apr Link to this post

    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. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 19 Apr Link to this post

    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 tinyint 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 sample 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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Andreas
    Andreas avatar
    6 posts
    Member since:
    Feb 2016

    Posted 20 Apr in reply to Dess Link to this post

    Hi Dess,

     

    thank you merry much, it's work perfectly.

    Regards,

    Andreas

Back to Top