按钮列的 DataGridView 图像 [英] DataGridView Image for Button Column
问题描述
我正在尝试向 datagridview 按钮列添加可点击的图像/按钮.
图像/按钮将是播放或停止的图标.如果用户单击播放按钮,系统上的服务将启动,如果用户单击停止按钮,则服务将停止.
我已经编写了用于启动和停止服务的函数.我遇到的困难是让按钮/图像显示在数据网格中并使其可点击.
这是我的代码:
this.dgrdServices.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);this.dgrdServices.Rows.Add();this.dgrdServices.Rows[0].Cells[0].Value = Image.FromFile(@"C:usersraddesktopgreen-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 = "卸载";
我不知道是使用图像按钮还是可点击的图像按钮更好.我也无法让按钮正确显示.
谢谢布拉德
在按钮上显示图像
您可以添加
<块引用>重要提示
在上面的例子中,我假设你在 someImage
成员中有一个图像变量:
Image someImage = Properties.Resources.SomeImage
确保在处理表单时处理 someImage
并避免在任何需要的地方直接使用 Properties.Resources.SomeImage
.
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.
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.
Here's what I have for code:
this.dgrdServices.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);
this.dgrdServices.Rows.Add();
this.dgrdServices.Rows[0].Cells[0].Value = Image.FromFile(@"C:usersraddesktopgreen-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.
Thanks Brad
Show Image On Button
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;
.
In the below code I suppose 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(someImage, new Rectangle(x, y, w, h));
e.Handled = true;
}
}
Show Image Without Button
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 = someImage, Name = "someName", HeaderText = "Some Text"
});
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 = someImage;
}
You also can set Image
property of DataGridViewImageColumn
to an image, but the image will not show on new row.
Handle Click
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!");
}
If you handled CellContentClick
you should exactly click on image when you are using image column.
Screenshot
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:
Important Note
In above examples I assume you have an image in
someImage
member variable:Image someImage = Properties.Resources.SomeImage
Make sure you dispose
someImage
on disposal of the form and avoid usingProperties.Resources.SomeImage
directly everywhere you need.
这篇关于按钮列的 DataGridView 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!