Friday, August 31, 2012

Bit fields as text in Infragistics UltraWinGrid

If you receive a bit field from database and want to show some text (Yes/No in this example), instead of checkboxes here is the solution. First you need a class that implements the Infragistics.Win.IEditorDataFilter interface. Here is my implementation:
  1. using System;
  2. using System.Windows.Forms;
  3. using Infragistics.Win;
  4.  
  5. namespace dbMotion.DAT {
  6. internal class GridCheckBoxDataFilter:IEditorDataFilter {
  7. #region IEditorDataFilter Members
  8.  
  9. object IEditorDataFilter.Convert(EditorDataFilterConvertArgs args) {
  10. switch (args.Direction) {
  11. case ConversionDirection.EditorToOwner:
  12. args.Handled = true;
  13. args.IsValid = true;
  14. CheckState state = (CheckState) args.Value;
  15. switch (state) {
  16. case CheckState.Checked:
  17. return "Yes";
  18. case CheckState.Unchecked:return "No";
  19. case CheckState.Indeterminate:return string.Empty;
  20. } break;
  21. case ConversionDirection.OwnerToEditor:args.Handled = true;
  22. args.IsValid = true;
  23. return args.Value.ToString() == bool.TrueString ? "Yes" : "No";
  24. case ConversionDirection.DisplayToEditor:args.Handled = true;
  25. args.IsValid = true;
  26. return args.Value.ToString() == "Yes";
  27. case ConversionDirection.EditorToDisplay:args.Handled = true;
  28. args.IsValid = true;
  29. return args.Value;
  30. } throw new Exception("Invalid value passed into GridChecBoxDataFilter.Convert()");
  31. }
  32.  
  33. #endregion
  34. }
  35. }
  36.  
And after this add the following code to the "InitializeLayout" event of your UltraWinGrid:
  1. private void Grid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
  2. {
  3. e.Layout.Bands[0].Columns["Col1"].Editor = new EditorWithText();
  4. e.Layout.Bands[0].Columns["Col1"].Style = ColumnStyle.FormattedText;
  5. e.Layout.Bands[0].Columns["Col1"].Editor.DataFilter = new GridCheckBoxDataFilter();
  6. }
  7. }
  8.  

Tuesday, March 6, 2012

Multiselect dropdown/combobox

In my current project i need a dropdown list where user can pick multiple items and then, after double-clickin on any of selected items, all of them appears in the tex area of dropdown. There is no standard controls that are offering this functionality, so i did it by hand. It's not something uniqe, i saw some samples on the web, but thi one is done using Infragistics controls for Windows forms and is relatively easy to implement. So here is the code (just one file)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinListView;

namespace MyControls
{
    public partial class MultiSelectComboBox : UltraTextEditor
    {
        private UltraListView lv;


        public MultiSelectComboBox()
        {
            InitializeComponent();
            SetTextEditorProperties();
        }

        public MultiSelectComboBox(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            SetTextEditorProperties();
        }

        public UltraListViewItemsCollection ListItems
        {
            get { return lv.Items; }
        }

        private void SetTextEditorProperties()
        {
            DropDownEditorButton button = new DropDownEditorButton();
            lv = new UltraListView();
            lv.AutoSize = false;
            lv.Width = Width;
            lv.View = UltraListViewStyle.List;
            lv.ViewSettingsList.ImageSize = Size.Empty;
            lv.ViewSettingsList.ColumnWidth = Width;
            lv.ViewSettingsList.MultiColumn = false;
            lv.DoubleClick += lv_DoubleClick;
            lv.Width = Size.Width;
            button.ButtonStyle = Infragistics.Win.UIElementButtonStyle.Office2010ScrollbarButton;
            button.Control = lv;
            ButtonsRight.Add(button);
            Enabled = true;
            UseFlatMode = DefaultableBoolean.True;
        }

        private void lv_DoubleClick(object sender, EventArgs e)
        {
            UltraListView listView = sender as UltraListView;
            if (listView == null)
            {
                return;
            }
            string result = string.Empty;
            foreach (var item in listView.Items)
            {
                if (!string.IsNullOrEmpty(item.Text) && item.IsSelected)
                {
                    result += item.Text + ";";
                }
            }
            Text = result.Remove(result.Length - 1, 1);
            DropDownEditorButton btn = ButtonsRight[0] as DropDownEditorButton;
            if (btn != null)
            {
                btn.CloseUp();
            }
        }

        #region Methods

        public void LoadControl(List<string> items)
        {
            lv.Items.Clear();
            lv.Width = Size.Width;
            Enabled = false;
            if (items == null)
            {
                return;
            }
            if (items.Count == 0)
            {
                return;
            }
            Enabled = true;
            try
            {
                lv.Items.Add(new UltraListViewItem(string.Empty));
                foreach (var item in items)
                {
                    if (!lv.Items.Exists(item))
                    {
                        lv.Items.Add(item, item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void LoadControl(object[] items)
        {
            try
            {
                List<string> list = new List<string>();
                list.AddRange(items.Select(t => t.ToString()));
                LoadControl(list);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion
    }
}