Datagrid and pictures

  • Thread starter Thread starter Bart
  • Start date Start date
B

Bart

Hi,
I would like to embedd a picture into a Datagrid cell.
Does anyone know of any good articles or tutorials on this.
Most of the stuff online is for the web (ASP.NET) and I
have done that already. I am looking for an idiomatic
solution for a Windows form.
 
Hi.
I already invest time and money in doing so. Investigate this code:

(e-mail address removed)

using System;

using System.Drawing;

using System.Windows.Forms;



namespace RHSoft.UI {

/// <summary>

/// Esta coluna permite alternar entre várias imagens passadas por parametro

/// através de uma ImageList. A cada imagem faz corresponder um valor

/// na coluna de dados. Todos os valores possíveis serão tambem passados

/// por parâmetro no construtor da classe. A cada imagem na posição 'i' da

/// ImageList corresponderá o valor na posição 'i' do dito array de valores

/// possiveis. É de salientar que o array de valores possiveis deverá conter

/// o mesmo nº de valores que a lista de imagens da ImageList.

/// <seealso cref="DataGridImageColumn"/>

/// </summary>

public class DataGridImageSwitchColumn :
System.Windows.Forms.DataGridColumnStyle {

private System.Windows.Forms.ImageList imlImages;

private System.Collections.ArrayList arlValues;

private bool focused = false;

private KeyEventHandler handlerkeyDown = null;

private MouseEventHandler handlerMouseDown = null;

/// <summary>

/// Construtor que configura a coluna para alternar entre imagens passadas

/// por parâmetro através da ImageList

/// </summary>

public DataGridImageSwitchColumn(ImageList imlImages, object[] values) {

this.imlImages = imlImages;

arlValues = new System.Collections.ArrayList(values);

handlerkeyDown = new KeyEventHandler(dataGrid_KeyDown);

handlerMouseDown = new MouseEventHandler(dataGrid_MouseDown);

}



protected override void ConcedeFocus() {

base.ConcedeFocus();

Focused = false;

}

// initiates a request to complete an editing procedure.

protected override bool Commit(System.Windows.Forms.CurrencyManager source,
int rowNum) {

Focused = false;

return true;

}

protected override void Abort(int rowNum) {

}


// Prepares the cell for editing a value.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly) {

this.Edit(source, rowNum, bounds, readOnly, "", false);

}

// Prepares the cell for editing using the specified CurrencyManager, row
number, and Rectangle parameters.

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText) {

this.Edit(source, rowNum, bounds, readOnly, instantText, false);

}

protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText, bool
cellIsVisible) {

Focused = cellIsVisible;

}

protected override int GetMinimumHeight() {

return 20;

}

protected override int GetPreferredHeight(Graphics g, object value) {

return this.DataGridTableStyle.PreferredRowHeight;

}

protected override Size GetPreferredSize(Graphics g, object value) {

return new Size(this.DataGridTableStyle.PreferredColumnWidth,
this.DataGridTableStyle.PreferredRowHeight);

}

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager
source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight) {

try {

bool lfocused = rowNum == source.Position && focused;

g.SetClip(bounds);

g.FillRectangle(backBrush, bounds);

if (lfocused) {

System.Windows.Forms.ControlPaint.DrawFocusRectangle(g, bounds);

}

object data = this.GetColumnValueAtRow(source, rowNum);

if (data != System.DBNull.Value) {

int index = arlValues.IndexOf(data);

if (index >= 0) {

Image image = imlImages.Images[index];

//imlImages.Draw(g, bounds.X+((bounds.Width-image.Width)>>1),
bounds.Y+((bounds.Height-image.Height)>>1), index);

g.DrawImageUnscaled(image, bounds.X+((bounds.Width-image.Width)>>1),
bounds.Y+((bounds.Height-image.Height)>>1));

}

}

}

catch(Exception) {

}

finally {

g.ResetClip();

}

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum, bool alignToRight)
{

Paint(g, bounds, source, rowNum, new System.Drawing.SolidBrush(((rowNum % 2)
0) ? this.DataGridTableStyle.AlternatingBackColor :
this.DataGridTableStyle.BackColor),
System.Drawing.SystemBrushes.ControlText, alignToRight);

}

protected override void Paint(Graphics g, Rectangle bounds,
System.Windows.Forms.CurrencyManager source, int rowNum) {

Paint(g, bounds, source, rowNum, false);

}

protected override void SetDataGridInColumn(System.Windows.Forms.DataGrid
dataGrid) {

}

protected override void Dispose(bool disposing) {

base.Dispose(disposing);

if (disposing) {

this.Focused = false;

handlerkeyDown = null;

arlValues = null;

imlImages = null;

}

}

private bool Focused {

get {

return this.focused;

}

set {

if (this.focused == value) {

return;

}

this.focused = value;

if (this.DataGridTableStyle.DataGrid.CanFocus && !this.ReadOnly) {

if (value) {

this.DataGridTableStyle.DataGrid.KeyDown += handlerkeyDown;

this.DataGridTableStyle.DataGrid.MouseDown += handlerMouseDown;

this.DataGridTableStyle.DataGrid.Focus();

}

else {

this.DataGridTableStyle.DataGrid.KeyDown -= handlerkeyDown;

this.DataGridTableStyle.DataGrid.MouseDown -= handlerMouseDown;

}

}

}

}

/// <summary>

/// Alternância de valores, para a frente ou para trás

/// dependendo da tecla CONTROL.

/// </summary>

private void SwitchValue(int numRow) {

System.Windows.Forms.CurrencyManager cm =
(System.Windows.Forms.CurrencyManager)this.DataGridTableStyle.DataGrid.Bindi
ngContext[this.DataGridTableStyle.DataGrid.DataSource];

int index;

object value = this.GetColumnValueAtRow(cm, cm.Position);

if (numRow != cm.Position && numRow != int.MinValue) {

return;

}

if (value != null && (index = arlValues.IndexOf(value)) >= 0) {

if (Control.ModifierKeys == Keys.Control) {

index = (index == 0) ? (arlValues.Count-1) : (index - 1);

}

else {

index = (index == arlValues.Count-1) ? 0 : (index + 1);

}

}

else {

index = (Control.ModifierKeys == Keys.Control) ? (arlValues.Count-1) : 0;

}

this.SetColumnValueAtRow(cm, cm.Position, arlValues[index]);

this.Invalidate();

this.ColumnStartedEditing(this.DataGridTableStyle.DataGrid);

this.DataGridTableStyle.DataGrid.Focus();

}

private void dataGrid_KeyDown(object sender, KeyEventArgs e) {

if (e.KeyCode == Keys.Space && !e.Alt) {

SwitchValue(int.MinValue);

}

}

private void dataGrid_MouseDown(object sender, MouseEventArgs e) {

if (e.Button == MouseButtons.Left) {

System.Windows.Forms.DataGrid.HitTestInfo hti =
this.DataGridTableStyle.DataGrid.HitTest(e.X, e.Y);

int columnIndex = this.DataGridTableStyle.GridColumnStyles.IndexOf(this);

if (hti.Column == columnIndex) {

SwitchValue(hti.Row);

}

hti = null;

}

}

}

}
 
Back
Top