How do you add an image to a single specific cell in a DataGridView?

人走茶凉 提交于 2019-12-11 06:07:07

问题


Using C# and Visual Studio, I have a DataGridView with 2 columns. For every row, the first column will display text. For every row EXCEPT one specific row, the second column will display text. In one specific cell in the second column, I need to show an image.

For example:

Row[0].Cell[0] = "test"  Row [0].Cell[1] = "test"
Row[1].Cell[0] = "test"  Row [1].Cell[1] = "test"
Row[2].Cell[0] = "test"  Row [2].Cell[1] = need to display an image here
Row[3].Cell[0] = "test"  Row [3].Cell[1] = "test"

回答1:


There is more than one way to do it but here is a simple example that will set one single cell to show an image:

    Bitmap bmp = (Bitmap) Bitmap.FromFile(someimagefile);

    DataGridViewImageCell iCell = new DataGridViewImageCell();
    iCell.Value = bmp;
    dataGridView1[1, 2] = iCell;

Of course any other image source will work as well..

Try not to leak the bitmaps if you change them..



来源:https://stackoverflow.com/questions/38834443/how-do-you-add-an-image-to-a-single-specific-cell-in-a-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!