简体   繁体   中英

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

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"

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM