Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been trying to solve this for a week now. Keep having handling a custom gridviewcell. here the problem, I want to have a cell in my datagridview that is a user control. I want a be able to use my control and save the final result in the currentcell (row,column). I can use either the TAG or the Value it dosen't matter.

Finally here's my code : Good luck it give it my headache.

C#
public class MultiBoxListCell : DataGridViewTextBoxCell
{
    Dictionary<int,>>> MycacheSaved;
    public MultiBoxListCell(): base()
    {

    }
    protected override void OnEnter(int rowIndex, bool throughMouseClick)
    {
        this.OwningRow.Height = 120;
        base.OnEnter(rowIndex, throughMouseClick);
    }
    protected override void OnLeave(int rowIndex, bool throughMouseClick)
    {
        this.OwningRow.Height = 22;
        base.OnLeave(rowIndex, throughMouseClick);
    }


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

        //I have declare my control
        MultiBoxListEditingControl CTRL = this.DataGridView.EditingControl as MultiBoxListEditingControl;

        //get the information from the value of the current row at position 9 (cell needed)
        List<package> DPack = this.DataGridView.Rows[rowIndex].Cells[9].Value as List<package>;

        //Show the control with this value only (that value is suposse to be different from a row to another row if modification have been made)
        CTRL.ShowPackages(DPack);


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

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that MultiBoxListCell contains.
            return typeof(List<package>);
        }
    }

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


C#
public class MultiBoxListEditingControl : MultiBoxList, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        bool valueChanged = false; 
        int rowIndex;

        public MultiBoxListEditingControl()
        {
            this.NameText = "Packages: "; 
        }

        public void ShowPackages(List<package> Packages)
        { 
            //we want to display the custom control and it's contain depending on the row (send from MultiBoxListCell)
            //now we need to display it to the user.

           // Fill items 
          List<boxitem> Boxitems =  BoxPackage.BOXPackages(Packages);
          FillItems(Boxitems); 
        }
        public override void FillItems(List<boxitem> items)
        {
            List<boxitem> BottomList = new List<boxitem>();

            //Fill item has it's own management system you give him a list and it work with it.
            //this fill the First list 
            base.FillItems(items);

            foreach (BoxItem selecteditem in items)
            {
                if (selecteditem.selected)
                {
                    BottomList.Add(selecteditem);
                }
            }
            //Fill the selecteditems to show to the user from the list send 
            base.FillItemsAlreadySelected(BottomList);
           
        }
        
        public object EditingControlFormattedValue
        {
            get
            {
                //HOW DO I FILL MY CURRENT CELL ??! With a e.g: List<string> 

               // dataGridView.CurrentCell.Value = this.GetCompletePackagesInfo();
               // dataGridView.Rows[rowIndex].Cells[9].Value = this.GetCompletePackagesInfo(); 
               return this.GetCompletePackagesInfo(); 
            }
            set
            {

            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
 
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }
        protected override void OnValueChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell have changed.
            valueChanged = true;
           // this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }


[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 10-Feb-12 21:04pm
v3
Comments
Michel [mjbohn] 10-Feb-12 17:33pm    
code tags added
OriginalGriff 11-Feb-12 3:05am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

BTW: You don't need to start your subject line with "Help me!" - we know you need help: why else would you be posting a question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900