Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've added an user control inside a datagridview row using a Custom Cell. Then I tried to pass a button click from the user control to get the value of the clicked button.

This is what I tried.
C#
private void InsertRow(Vdine.PosApp.UI.Models.OrderModule.OrderManagementModel orderManagementModel, bool isInsertToTop)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.Height = 40;

            row.DefaultCellStyle.SelectionForeColor = Color.Black;

            DataGridViewCell cellFoodName = new DataGridViewTextBoxCell();
            cellFoodName.Value = orderManagementModel.FoodName;
            row.Cells.Add(cellFoodName);

            if (orderManagementModel.OrderDetail != null)
            {
                DataGridViewCell cellQty = new DataGridViewTextBoxCell();
                cellQty.Value = orderManagementModel.OrderDetail.Quantity;
                row.Cells.Add(cellQty);

                FoodCoursingUserControl foodCoursingUserControl = new FoodCoursingUserControl();                
                foodCoursingUserControl.Visible = true;                
                CustomeCell customCell = new CustomeCell();
                customCell.Value = foodCoursingUserControl;               

                DataGridViewCell cellOrderCoursingId = customCell; //// new DataGridViewTextBoxCell();
                cellOrderCoursingId.Value = customCell.Value;
                row.Cells.Add(cellOrderCoursingId);

                DataGridViewCell cellOrderDetailId = new DataGridViewTextBoxCell();
                cellOrderDetailId.Value = orderManagementModel.OrderDetail.OrderDetailID;
                row.Cells.Add(cellOrderDetailId);

            }
            else
            {
                cellFoodName.Style.Font =
                    new Font(this.orderdetailsDataGridView.DefaultCellStyle.Font, FontStyle.Bold);
                row.DefaultCellStyle.BackColor = Color.Orange;

                DataGridViewCell cellQty = new DataGridViewTextBoxCell();
                cellQty.Value = string.Empty;
                row.Cells.Add(cellQty);

                FoodCoursingUserControl foodCoursingUserControl = new FoodCoursingUserControl();
                foodCoursingUserControl.Visible = true;
                CustomeCell customCell = new CustomeCell();
                customCell.Value = foodCoursingUserControl;

                DataGridViewCell cellOrderCoursingId = customCell;  ////new DataGridViewTextBoxCell();
                cellOrderCoursingId.Value = customCell.Value;
                row.Cells.Add(cellOrderCoursingId);

                DataGridViewCell cellOrderDetailId = new DataGridViewTextBoxCell();
                cellOrderDetailId.Value = string.Empty;
                row.Cells.Add(cellOrderDetailId);

            }

            this.orderdetailsDataGridView.Rows.Insert(isInsertToTop ? 0 : this.orderdetailsDataGridView.Rows.Count, row);
        }


Then in the cellclick event,
C#
/// <summary>
        /// Handles the CellClick event of the OrderdetailsDataGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellEventArgs"/> instance containing the event data.</param>
        private void OrderdetailsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string commandName = this.orderdetailsDataGridView.Columns[e.ColumnIndex].HeaderText;

                string orderDetailId = this.orderdetailsDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();

                FoodCoursingUserControl cntrl = this.orderdetailsDataGridView.Rows[e.RowIndex].Cells[2].Value as FoodCoursingUserControl;


                if (string.IsNullOrEmpty(orderDetailId))
                {
                    if (commandName == "Order Coursing")
                    {

                    }
                }
                else
                {
                    DataGridViewCell coursingCell = (DataGridViewCell)this.orderdetailsDataGridView.Rows[e.RowIndex].Cells[2];

                    if (commandName == "Order Coursing")
                    {

                        string selectedId = (string)cntrl.Tag;

                    }

                }
            }
        }


Here FoodCoursingUserControl has some buttons in it and it is a child control in the DataGridView control, once I clicked the CellClick its only fire the Parent control event. I want to fire the button click event on the FoodCoursingUserControl to get the Tag property of the button.

Can anyone help me on this?
Thanks in advance.
Posted
Updated 9-Jun-14 4:12am
v2

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