2016-03-27 66 views
2

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

图像/按钮将成为播放或停止的图标。如果用户点击播放按钮,则启动系统上的服务,如果用户单击停止按钮,服务将停止。

我已经编写了启动和停止服务的函数。我遇到的困难是让按钮/图像显示在数据网格中并使其可点击。

这里就是我有下面的代码:

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"; 

我不能工作了,如果这将是最好使用一个按钮,用于为图像或只是那是可点击的图像。我也无法让按钮正确显示。

感谢 布拉德

+0

http://www.codeproject.com/KB/grid/DGV_ImageButtonCell.aspx – MethodMan

+0

所以,当你说它没有正确显示时,它是否根本没有出现?它显示,但显示器有问题吗? – Jace

+0

我已经尝试了各种可以找到的示例代码。我最终会看到一个没有标签的灰色按钮,或者是一个灰色的按钮,上面写着windows.system.bitmap。当我按下按钮来调用启动/停止服务的功能时,我也无法解决问题。 – Brad

回答

5

显示图像在按钮

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

在下面的代码中,我认为你有一个像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; 
    } 
} 

显示图像,而按钮

要显示所有列的单个图像,包括新行,您可以设置DataGridViewImageColumnImage财产。这样,图像将在该列显示在所有行中:如果你可能想为细胞不同的图像

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

此外,您还可以在CellFormatting事件设置的DataGridViewImageColumn格式化的值:

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

您还可以将DataGridViewImageColumnImage属性设置为图像,但图像不会显示在新行上。

手柄点击

要处理点击图片/按钮,您可以处理CellClickCellContentClick事件:

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你应该对图像准确点击,当你使用image列。

截图

下面是结果。第一列是示出图像和第二列的按钮列是一个正常的图像列设定为显示单个图像:

enter image description here

+0

我得到了这个工作,但有几个意见 - 现在我看到一个按钮上面有一个图像。我真正想看到的只是形象。图片需要点击。 – Brad

+0

它是一个按钮列,显示您正在查找的图像*我正在尝试将一个可点击的图像/按钮添加到datagridview按钮列中* –

+0

如果您只需要图像,则可以简单地使用“DataGridViewImageColumn” 。 –