Bilder in DGV einfuegen



  • Hallo,

    mal wieder eine Frage von mir bezüglich eines DataGridViews.

    Gibt es eine Möglichkeit, in einzelne Zellen eines DGVs Bilder einzufügen? Und wenn ja, wie muss man dabei vorgehen?

    Ein paar weitere Infos zu meinem Problem. Das DGV besteht aus beliebig vielen Rows und mehreren Columns. Die Columns sind vom Typ "TextBoxColumn" und entsprechend sind die Inhalte Texte. Nur in Ausnahmefällen soll ein Bild (in diesem Fall z.B. ein INFO-Icon) in eine Zelle eingefügt werden. Leider habe ich nicht mal einen Ansatz, wie ich dieses realisieren soll.

    Bin für jeden Ansatz/Hilfe dankbar....



  • Schau dir mal das "CellPainting" Event vom DataGridView an.
    Das wird aufgerufen, wenn eine Zelle gezeichnet wird.
    Da kann man dann reinzeichnen was man will, u. a. auch Bilder.

    Siehe auch: http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting(VS.80).aspx

    Ist zwar C# Code, aber sollte kein Problem sein, da C++/CLI draus zu machen.

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    		{
    			// Paint the bitmap only in the first cell of first row
    			if (e.ColumnIndex != 0 || e.RowIndex != 0)
    			{return;}
    
    			Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor);
    			Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
    			Pen gridLinePen = new Pen(gridBrush);
    
    			// Erase the cell.
    			e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
    
    			// Draw the grid lines (only the right and bottom lines;
    			// DataGridView takes care of the others).
    			e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
    			e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
    
    			// Draw the bitmap
    			e.Graphics.DrawImage(m_bitmap, new Point(e.CellBounds.Left + 5, e.CellBounds.Top + 5));
    
    			// Draw the text content of the cell, ignoring alignment.
    			if (e.Value != null)
    			{
    				e.Graphics.DrawString
    				(
    					(String)e.Value,
    					e.CellStyle.Font,
    					Brushes.Crimson,
    					e.CellBounds.X + m_bitmap.Width + 5, // draw text aside the bitmap
    					e.CellBounds.Y + 2,
    					StringFormat.GenericDefault
    				);
    			}
                e.Handled = true;
    		}
    

Anmelden zum Antworten