Click here to Skip to main content
15,910,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a user control which is the combination of a textbox control and a label control.
Here is the code:

C#
public class UC_AccLedgerInfo: TableLayoutPanel
    {
        public TextBox tb_AccLedgerName;
        public Label lbl_CurrBal;
        string _ledgerName;


        public UC_AccLedgerInfo()
        {
            InitializeTLP();
        }

        void InitializeTLP()
        {            
            this.ColumnCount = 1;
            this.RowCount = 2;
            this.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
            this.AutoSize = true;
            this.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            this.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            AddControls();
        }
        void AddControls()
        {
            TBLedgerName();
            LBLCurrBal();
        }

        void TBLedgerName()
        {
            if (tb_AccLedgerName == null)
                tb_AccLedgerName = new TextBox();            
            tb_AccLedgerName.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            tb_AccLedgerName.BackColor = Color.AliceBlue;
            this.Controls.Add(tb_AccLedgerName, 0, 0);
        }
        public string LedgerName 
        {
            get
            {
                return _ledgerName;
            }
            set
            {
                _ledgerName = value;
            }
        }

        void LBLCurrBal()
        {
            if (lbl_CurrBal == null)
                lbl_CurrBal = new Label();
            lbl_CurrBal.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            lbl_CurrBal.Text = String.Format("Curr Bal: {0}", "2000");
            lbl_CurrBal.BackColor = Color.AliceBlue;
            this.Controls.Add(lbl_CurrBal, 0, 1);
        }       
}



I have a DataGridView(DGV) on form on which I need to host this control.
I created a custom DataGridViewEditingControl class for my user control.
Also, respective DataGridViewCell class. The code for cell is:

C#
class DGVAccLedgerInfoCell : DataGridViewCell
   {
       public override Type EditType
       {
           get
           {
               // Return the type of the editing control the cell uses.
               return typeof(DGVAccLedgerInfoEditingControl);
           }
       }
       public override Type ValueType
       {
           get
           {
               // Return the type of the value that NumericCell contains.
               return typeof(String);
           }
       }
   }


For this cell class, nothing is displayed on the dgv in the respective cells since the paint method is not specified above for the cell class.

One way I found from MSDN How to: Host Controls in Windows Forms DataGridView Cells[^]

But in the example given in the link, they used DataGridViewTextBoxEditingCell to render the paint event for the calender. This will not work for my user control since it is a combination of controls which is to be displayed as it is.

With my code, DGV hosts nothing in the respective cell since no paint event specified. I dont know how to specify the paint event for user control.
Please guide.
Thank you!

What I have tried:

I got these links after spending considerable time on google:
How to: Host Controls in Windows Forms DataGridView Cells[^]

c# - How to paint custom control on datagridviewcell? - Stack Overflow[^]
Posted

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