如何在C#中的DataGridView中添加Picturebox控件

发布于 2025-02-09 23:42:06 字数 2191 浏览 2 评论 0原文

private void InitializeComponent()
{
    dataGrid = new DataGridView();
    dataGrid.DataSource = this._lstWorklet;
    dataGrid.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(dataGrid_DataBindingComplete);
}

private void dataGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    PictureBox _btnView = new PictureBox();
    _btnView.Size = new System.Drawing.Size(24, 24);
    _btnView.BackgroundImage = ProcessingDataModel._viewImg;
    _btnView.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
    _btnView.Cursor = System.Windows.Forms.Cursors.Hand;
    _btnView.Click += new System.EventHandler(this.btnViewRecording_click);
    System.Windows.Forms.ToolTip _toolTipView = new System.Windows.Forms.ToolTip();
    _toolTipView.SetToolTip(_btnView, "View Recording");


    PictureBox _btnDelete = new PictureBox();
    _btnDelete.Size = new System.Drawing.Size(24, 24);
    _btnDelete.BackgroundImage = ProcessingDataModel._deleteImg;
    _btnDelete.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
    _btnDelete.Cursor = System.Windows.Forms.Cursors.Hand;
    _btnDelete.Click += new System.EventHandler(this.btnDeleteRecording_click);
    System.Windows.Forms.ToolTip _toolTipDelete = new System.Windows.Forms.ToolTip();
    _toolTipDelete.SetToolTip(_btnDelete, "Delete Recording");

    dataGrid.Columns[3].Name = "Action";
    dataGrid.Columns[3].HeaderText = "Action";
    dataGrid.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

    for (int i = 0; i <= dataGrid.Rows.Count; i++)
    {
       dataGrid.Rows[i].Cells[3].Controls.Add(_btnView);
       dataGrid.Rows[i].Cells[3].Controls.Add(_btnDelete);
    }
}

我会在(datagrid.rows [i] .cells [3] .controls.add(_btnview))上遇到错误的错误,例如'datagridViewCell'不包含“控件”的定义,没有可访问的扩展方法'可以找到控制“接受“ datagridviewcell”类型的第一个参数”(您是否缺少使用指令或汇编引用?) 因此,请帮助我将platuerbox添加到Windows应用程序的DataGridView中的列中。

private void InitializeComponent()
{
    dataGrid = new DataGridView();
    dataGrid.DataSource = this._lstWorklet;
    dataGrid.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(dataGrid_DataBindingComplete);
}

private void dataGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    PictureBox _btnView = new PictureBox();
    _btnView.Size = new System.Drawing.Size(24, 24);
    _btnView.BackgroundImage = ProcessingDataModel._viewImg;
    _btnView.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
    _btnView.Cursor = System.Windows.Forms.Cursors.Hand;
    _btnView.Click += new System.EventHandler(this.btnViewRecording_click);
    System.Windows.Forms.ToolTip _toolTipView = new System.Windows.Forms.ToolTip();
    _toolTipView.SetToolTip(_btnView, "View Recording");


    PictureBox _btnDelete = new PictureBox();
    _btnDelete.Size = new System.Drawing.Size(24, 24);
    _btnDelete.BackgroundImage = ProcessingDataModel._deleteImg;
    _btnDelete.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
    _btnDelete.Cursor = System.Windows.Forms.Cursors.Hand;
    _btnDelete.Click += new System.EventHandler(this.btnDeleteRecording_click);
    System.Windows.Forms.ToolTip _toolTipDelete = new System.Windows.Forms.ToolTip();
    _toolTipDelete.SetToolTip(_btnDelete, "Delete Recording");

    dataGrid.Columns[3].Name = "Action";
    dataGrid.Columns[3].HeaderText = "Action";
    dataGrid.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

    for (int i = 0; i <= dataGrid.Rows.Count; i++)
    {
       dataGrid.Rows[i].Cells[3].Controls.Add(_btnView);
       dataGrid.Rows[i].Cells[3].Controls.Add(_btnDelete);
    }
}

I am getting error in for loop on (dataGrid.Rows[i].Cells[3].Controls.Add(_btnView)) like 'DataGridViewCell' does not contain a definition for 'Controls' and no accessible extension method 'Controls' accepting a first argument of type 'DataGridViewCell' could be found (are you missing a using directive or an assembly reference?)
So please help me about adding the Picturebox to the column in datagridview for windows application.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文