【问题标题】:Button click event not firing from custom DataGridViewCell按钮单击事件未从自定义 DataGridViewCell 触发
【发布时间】:2010-01-14 10:34:10
【问题描述】:

我创建了自定义列(DataGridViewButtonControlColumn)和单元格(ButtonControlCell)类来保存 System.Windows.Forms.Button 控件。按钮被添加到列中并正确显示。 在将按钮设置为 ButtonControlCell 的值之前,我为“单击”附加了一个事件处理程序。但是单击按钮时不会调用此处理程序。

我在重写的 Paint 函数中将按钮添加到 DataGridView 的控件中。

是否有任何特定的步骤我必须按照 DataGridView 注册 Button?

代码:

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();
            }
        }
.
.
}

在实现中:

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);
        }

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

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

    【讨论】:

    • 我正在使用 DataGridViewButtonColumn,但它的行为并不符合我的意愿。控件的大小被调整以填充单元格,我找不到阻止这种情况的属性。顺便说一句,我暂时使用 DataGridViewLinkColumn,直到我弄清楚如何触发点击事件
    【解决方案2】:

    用于实现此目的的方法是捕获在 DataGrid 上执行的事件(例如按钮单击),获取控件并根据该事件执行任何其他任务。 通过阅读有关自定义 DataGridView 的几个教程找到了答案。

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2010-10-21
      • 2013-01-17
      • 2012-03-14
      • 2011-12-02
      • 2013-10-14
      相关资源
      最近更新 更多