按钮列的DataGridView图像 [英] DataGridView Image for Button Column

查看:218
本文介绍了按钮列的DataGridView图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个可点击的图像/按钮到datagridview按钮列。

I'm trying to add a clickable image/button to a datagridview button column.

图像/按钮将是一个用于播放或停止的图标。如果用户单击播放按钮,系统上的服务开始,如果用户单击停止按钮,则停止服务。

The image/button will be an icon for play or stop. If the user clicks the play button a service on the system is started, if the user clicks the stop button a service is stopped.

我已经有开始和停止服务的书面功能。我遇到困难是获取按钮/图像显示在datagrid并使其可点击。

I already have written functions for starting and stopping the service. What I'm having difficulty with is getting the button/image to show up in the datagrid and making it clickable.

这是我的代码:

this.dgrdServices.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);
this.dgrdServices.Rows.Add();
this.dgrdServices.Rows[0].Cells[0].Value = Image.FromFile(@"C:\users\brad\desktop\green-dot.gif"); 
this.dgrdServices.Rows[0].Cells[1].Value = "MyServer";
this.dgrdServices.Rows[0].Cells[2].Value = "MyService";
this.dgrdServices.Rows[0].Cells[3].Value = "Started";
this.dgrdServices.Rows[0].Cells[4].Value = new DataGridViewButtonCell();
this.dgrdServices.Rows[0].Cells[5].Value = "Uninstall";

如果最好使用图像或只是一个可点击的图像。我也无法得到按钮正确显示。

I can't work out if it would be better to use a button which is an image or an just an image that's clickable. I also can't get a button to show up correctly.

感谢
Brad

Thanks Brad

推荐答案

按钮显示图像

您可以添加一个 DataGridViewButtonColumn ,然后处理 CellPainting 事件,并检查是否为您的按钮列引发事件,然后在其上绘制图像。在事件结束时,不要忘记设置 e.Handled = true;

You can add a DataGridViewButtonColumn, then handle CellPainting event of the grid and check if the event is raised for your button column, then draw an image on it. At the end of event, don't forget to set e.Handled = true;.

在下面代码我假设你有一个图像资源,如 SomeImage

In the below code I supposed you have an image resource like SomeImage:

private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0)
        return;

    //I supposed your button column is at index 0
    if (e.ColumnIndex == 0)
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.All);

        var w = Properties.Resources.SomeImage.Width;
        var h = Properties.Resources.SomeImage.Height;
        var x = e.CellBounds.Left + (e.CellBounds.Width - w) / 2;
        var y = e.CellBounds.Top + (e.CellBounds.Height - h) / 2;

        e.Graphics.DrawImage(Properties.Resources.SomeImage, new Rectangle(x, y, w, h));
        e.Handled = true;
    }
}

无按钮显示图像

要在包含新行的所有行上显示单个图像,您可以设置<$ c $的图像属性$ c> DataGridViewImageColumn 。这样,该图像将显示在所有行的列中:

To show a single image on all rows including new row, you can set the Image property of DataGridViewImageColumn. This way the image will be shown in that column on for all rows:

dataGridView1.Columns.Add(new DataGridViewImageColumn(){
    Image = Properties.Resources.SomeImage, Name = "someName", HeaderText = "Some Text"
});

另外,如果您想为单元格设置不同的图像,可以设置格式化的 DataGridViewImageColumn in CellFormatting 事件:

Also if you may want to have different images for cells, you can set the formatted value of DataGridViewImageColumn in CellFormatting event:

void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.RowIndex < 0)
        return;

    //I supposed the image column is at index 1
    if (e.ColumnIndex == 1)
        e.Value = Properties.Resources.SomeImage;
}

您还可以设置图像属性 DataGridViewImageColumn 到图像,但图像不会显示在新行。

You also can set Image property of DataGridViewImageColumn to an image, but the image will not show on new row.

处理单击

要处理单击图像/按钮,您可以处理 CellClick CellContentClick 事件:

To handle Click on image/button you can handle CellClick or CellContentClick event:

void grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0)
        return;

    //I suposed you want to handle the event for column at index 1
    if (e.ColumnIndex == 1)
        MessageBox.Show("Clicked!");
}

如果您处理 CellContentClick

截图

这是结果。第一列是显示图像的按钮列,第二列是设置为显示单个图像的正常图像列:

Here is the result. First column is a button column showing an image and second column is a normal image column set to show a single image:

这篇关于按钮列的DataGridView图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆