简体   繁体   中英

Button click event not firing from custom DataGridViewCell

I've created custom column(DataGridViewButtonControlColumn) and cell(ButtonControlCell) classes to hold System.Windows.Forms.Button controls. The buttons get added to the columns and get displayed properly. Before I set the button as the value of a ButtonControlCell, I attach an event handler for "Click". But this handler is not called when a button is clicked.

I add the button to the DataGridView's controls in the overridden Paint function.

Are there any specific steps which I have to follow to register the Button with the DataGridView?

Code:

public class ButtonControlCell : DataGridViewTextBoxCell
    {
.
.
.
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            if (btnVal != null)
            {
                btnVal.Size = new System.Drawing.Size(80, 20);
                btnVal.Location = cellBounds.Location;
                this.DataGridView.Controls.Add(btnVal);
            }            
        }
.
.
.
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            // This is not called when the button is clicked (which is correct I guess)
            base.OnMouseClick(e);

            if (btnVal != null)
            {
                btnVal.PerformClick();
            }
        }
.
.
}

In the implementation:

private void AddButtonCell(string sText, EventHandler oEh, DataGridViewButtonColumn oClm, DataGridView dgvParent, int iRow, int iColumn)
        {
            Button btnTemp = new Button();
            btnTemp.Height = 20;
            btnTemp.Width = 60;
            btnTemp.Anchor = AnchorStyles.Top;
            btnTemp.Text = sText;
            btnTemp.Click += new EventHandler(btnTemp_Click);
            btnTemp.Tag = new Point(iRow, iColumn);

            Controls.Add(btnTemp);

            dgvParent.Rows[iRow].Cells[iColumn].Value = btnTemp;
        }

        void btnTemp_Click(object sender, EventArgs e)
        {
            Button btnSender = (Button)sender;

            DataGridViewRow r = dgvResults.Rows[((Point)btnSender.Tag).X];

            TagInfo oRet = new TagInfo((string)r.Cells[iTitleColIndex].Value, (string)r.Cells[iArtistColIndex].Value,
                (string)r.Cells[iAlbumColIndex].Value);
            oRet.imgAlbumArt = (System.Drawing.Image)r.Cells[iArtColIndex].Tag;
            oParent.TagWithInfo(oRet, true);
        }

如果您的目标是只有一个按钮,则用户可以单击,则可以使用内置的DataGridViewButtonColumn( DataGridViewButtonColumn )。

The method used to achieve this is to catch an event (eg button click) performed on the DataGrid, get the control and perform any additional tasks depending on that. Found the answer by going through several tutorials on custom DataGridView s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM