DataGridView 图像按钮列

发布于 2024-11-19 03:52:25 字数 163 浏览 4 评论 0原文

我想在 datagridviews 列中添加图像按钮。我添加了 datagridview 和 datagridviewbuttonColumn 但我没有将图像设置为 this 。我想在 datagridviews 列中添加带有按钮的图像,然后单击此按钮时 datagridview 行编辑或删除。请问我该怎么做!

I want to add image button in datagridviews column . I added the datagridview with datagridviewbuttonColumn
but I don't set the image to this . I want to add image with button in datagridviews column and when click this button datagridview row edit or delete. Please How do I do that!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

后来的我们 2024-11-26 03:52:25

实现此目的的一种方法是简单地重写 DataGridViewButtonCell 中的 Paint 方法,并从图形参数中调用 DrawImage。该调用必须发生在碱基调用之后

public class DeleteCell : DataGridViewButtonCell {
        Image del = Image.FromFile("..\\..\\img\\delete.ico");
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            graphics.DrawImage(del, cellBounds);
        }
    }

之后,只需创建您自己的 DataGridViewButtonColumn,并将创建的 DeleteCell 设置为单元格模板:

public class DeleteColumn : DataGridViewButtonColumn {
        public DeleteColumn() {
            this.CellTemplate = new DeleteCell();
            this.Width = 20;
            //set other options here 
        }
    }

就是这样。现在为您的 DataGridView 使用 DeleteColumn:

dgvBookings.Columns.Add(new DeleteColumn());

如果按钮单击的结果操作取决于单元格行,请确保正确处理单击,即捕获单元格行索引。

One way this can be achieved is to simply override Paint method from DataGridViewButtonCell and and call DrawImage form the graphics parameter. The call must occur after base call.

public class DeleteCell : DataGridViewButtonCell {
        Image del = Image.FromFile("..\\..\\img\\delete.ico");
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            graphics.DrawImage(del, cellBounds);
        }
    }

After that simply create your own DataGridViewButtonColumn, and set created DeleteCell as the cell template:

public class DeleteColumn : DataGridViewButtonColumn {
        public DeleteColumn() {
            this.CellTemplate = new DeleteCell();
            this.Width = 20;
            //set other options here 
        }
    }

That's it. Now use DeleteColumn for your DataGridView:

dgvBookings.Columns.Add(new DeleteColumn());

If the resulting action of button click depends on cell row, make sure to handle the clicks right, that is, to catch the cell row index.

残龙傲雪 2024-11-26 03:52:25

虽然DataGridViewButtonColumn,但它并不直接提供显示图像的方法。

以下链接可以指导您逐步完成任务:

DataGridView 图像按钮单元格

希望这会有所帮助...

Though DataGridView has ButtonColumn it does not directly provides way to display images.

The following link may guide you step by step to achieve your task:

DataGridView Image Button Cell

Hope this helps...

日久见人心 2024-11-26 03:52:25

您可以使用从 DataGridViewImageButtonCell 类继承的自定义 DataGridViewImage 类,如下所示

public class DataGridViewImageButtonDeleteCell : DataGridViewImageButtonCell
{
    public override void LoadImages()
    {
       // Load them from a resource file, local file, hex string, etc.          
    }
}

另外,请检查 这个主题也是如此

You can use customize DataGridViewImage class that inhrit from DataGridViewImageButtonCell class as below

public class DataGridViewImageButtonDeleteCell : DataGridViewImageButtonCell
{
    public override void LoadImages()
    {
       // Load them from a resource file, local file, hex string, etc.          
    }
}

And also, check this topic as well

江湖正好 2024-11-26 03:52:25

只需创建带有图像列的数据表并向其中添加图像

dtMain.Columns.Add("ImageColumn", typeof(Image));

dtMain.Rows.Add(Image.FromFile(photopath + "1.jpg"));

然后在事件 dataGridViewMain_CellContentClick 上编写以下代码

  if (e.ColumnIndex == dataGridViewMain.Columns["ImageColumn"].Index)
  {
  lblShowCellData.Text = dataGridViewMain.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString();
  // Do some thing else....
  }

http://tablegridview.blogspot.in 下载完整代码

Simply create datatablewith image column and add image to it

dtMain.Columns.Add("ImageColumn", typeof(Image));

dtMain.Rows.Add(Image.FromFile(photopath + "1.jpg"));

Then On event dataGridViewMain_CellContentClick write following code

  if (e.ColumnIndex == dataGridViewMain.Columns["ImageColumn"].Index)
  {
  lblShowCellData.Text = dataGridViewMain.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString();
  // Do some thing else....
  }

Download full code at http://tablegridview.blogspot.in

亽野灬性zι浪 2024-11-26 03:52:25

可以在 asp:ButtonColumn 的 Text-Property 中提供 HTML。所以你实际上可以做这样的事情:

<asp:ButtonColumn Text="<img src='icons/delete.gif' border='0' title='Delete entry' >" CommandName="delete"></asp:ButtonColumn>

呈现如下(HTML):

<td>
  <a href="...">
    <img src='icons/delete.gif' border='0' title='Delete entry' />
  </a>
</td>

It is possible to provide HTML in the Text-Property of asp:ButtonColumn. So you can actually do something like this:

<asp:ButtonColumn Text="<img src='icons/delete.gif' border='0' title='Delete entry' >" CommandName="delete"></asp:ButtonColumn>

This is rendered as follows (HTML):

<td>
  <a href="...">
    <img src='icons/delete.gif' border='0' title='Delete entry' />
  </a>
</td>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文