Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
    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;
            }
        }
 
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 10-Feb-12 9:59am
Edited 10-Feb-12 22:04pm
(no name)1.1M
v3
Comments
Michel [mjbohn] at 10-Feb-12 17:33pm
   
code tags added
OriginalGriff at 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)

  Print Answers RSS
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 11 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100