Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to have custom control in DataGridView display object's value?

$
0
0
I have a sample project located here

The project has a main form `Form1` where the user can enter customers in a datagridview. The `CustomerType` column is a custom control and when the user clicks the button, a search form `Form2` pops up.

The search form is populated with a list of type `CustomerType`. The user can select a record by double-clicking on the row, and this object should be set in the custom control. The `DataGridView` should then display the `Description` property but in the background each cell should hold the value (ie. the `CustomerType` instance).

The relevant code is located in the following classes:

The column class:

    public class DataGridViewCustomerTypeColumn : DataGridViewColumn
    {
        public DataGridViewCustomerTypeColumn()
            : base(new CustomerTypeCell())
        { }

        public override DataGridViewCell CellTemplate
        {
            get { return base.CellTemplate; }
            set
            {
                if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomerTypeCell)))
                {
                    throw new InvalidCastException("Should be CustomerTypeCell.");
                }

                base.CellTemplate = value;
            }
        }
    }

The cell class:

    public class CustomerTypeCell : DataGridViewTextBoxCell
    {
        public CustomerTypeCell()
            : base()
        { }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            CustomerTypeSearch ctl = DataGridView.EditingControl as CustomerTypeSearch;

            if (this.Value == null)
                ctl.Value = (CustomerType)this.DefaultNewRowValue;
            else
                ctl.Value = (CustomerType)this.Value;
        }

        public override Type EditType
        {
            get { return typeof(CustomerTypeSearch); }
        }

        public override Type ValueType
        {
            get { return typeof(CustomerType); }
        }

        public override object DefaultNewRowValue
        {
            get { return null; }
        }
    }

And the custom control:

    public partial class CustomerTypeSearch : UserControl, IDataGridViewEditingControl
    {
        private DataGridView dataGridView;
        private int rowIndex;
        private bool valueChanged = false;
        private CustomerType value;

        public CustomerTypeSearch()
        {
            InitializeComponent();
        }

        public CustomerType Value
        {
            get { return this.value; }
            set
            {
                this.value = value;

                if (value != null)
                    textBoxSearch.Text = value.Description;
                else
                    textBoxSearch.Clear();
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();

            DialogResult dr = f.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                Value = f.SelectedValue;
            }
        }

        #region IDataGridViewEditingControl implementation

        public object EditingControlFormattedValue
        {
            get
            {
                if (this.value != null)
                    return this.value.Description;
                else
                    return null;
            }
            set
            {
                if (this.value != null)
                    this.value.Description = (string)value;
            }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.BorderStyle = BorderStyle.None;
            this.Font = dataGridViewCellStyle.Font;
        }

        public int EditingControlRowIndex
        {
            get { return rowIndex; }
            set { rowIndex = value; }
        }

        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            return false;
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
            //No preparation needs to be done
        }

        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }

        public DataGridView EditingControlDataGridView
        {
            get { return dataGridView; }
            set { dataGridView = value; }
        }

        public bool EditingControlValueChanged
        {
            get { return valueChanged; }
            set { valueChanged = value; }
        }

        public Cursor EditingPanelCursor
        {
            get { return base.Cursor; }
        }

        #endregion

        private void CustomerTypeSearch_Resize(object sender, EventArgs e)
        {
            buttonSearch.Left = this.Width - buttonSearch.Width;
            textBoxSearch.Width = buttonSearch.Left;
        }
    }

However, the `DataGridView` is not displaying the text and it also is not keeping the `CustomerType` value for each cell.

What am I missing?

Marketplace: [url=http://tinyurl.com/75gc58b]Itza[/url] - Review: [url=http://tinyurl.com/ctdz422]Itza Update[/url]


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>