i would like to show an image in a datagridview cell giving the path of the image.. how can i do so?

Recommended Answers

All 5 Replies

found it thx bye :)

Heres an exmample:

private Bitmap transparentImage;
        public Form1()
        {
            InitializeComponent();

            transparentImage = new Bitmap(20, 20);
            Graphics graphics = Graphics.FromImage(transparentImage);
            graphics.FillRectangle(Brushes.Transparent, 0, 0, 20, 20);
            graphics.Dispose();

            DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
            {
                imageColumn.Name = "imageColumn";
                imageColumn.HeaderText = "Images";
                imageColumn.Width = 100;
                imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
                imageColumn.CellTemplate = new DataGridViewImageCell(false);
                imageColumn.DefaultCellStyle.NullValue = null;
            }
            dataGridView1.Columns.Add("column1", "Column 1");
            dataGridView1.Columns.Insert(1, imageColumn);
            dataGridView1.Rows.Add("item 1", null);
            dataGridView1.Rows.Add("item 2", null);
            dataGridView1.AllowUserToAddRows = false;

            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    Bitmap result = new Bitmap(100, 100);
                    using (Graphics g = Graphics.FromImage((Image)result))
                        g.DrawImage(new Bitmap(open.FileName), 0, 0, 100, 100);
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = result;
                }
            }
        }

there has some code to be done on the image resizing, but this works. Not you can insert an image from hard disk, with OpenFileDialog() method. And you can only select it ones (31 row - comparison to null value of the cell - you can change that, or remove).

commented: Nice. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.