首页 新闻 会员 周边 捐助

winform datagridview 添加图片按钮

0
悬赏园豆:10 [已解决问题] 解决于 2020-01-02 09:36

winform datagridview 中其中有一列为DataGridViewButtonColumn
想要在 DataGridViewButtonColumn 上 做成一个图片按钮
应该如何添加上图片按钮呢?

撞到南墙拆了继续走的主页 撞到南墙拆了继续走 | 初学一级 | 园豆:99
提问于:2019-12-31 14:02
< >
分享
最佳答案
0
设置:e.Handled = true;

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

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

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

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

    if (e.ColumnIndex == 1)
        e.Value = someImage;
}

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

    if (e.ColumnIndex == 1)
        MessageBox.Show("Clicked!");
}
收获园豆:10
悟行 | 专家六级 |园豆:12559 | 2020-01-02 08:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册