468,525 Members | 2,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

Datagrid Button

Someone has implemented a Datagrid Button for the Windows.Form?
Nov 15 '05 #1
3 5767
It's pretty complicated to implement custom datagridcolumnstyles that are
unbound to data.

This is a DataGridButtonColumn prgrammed by me.

using System;

using System.Collections;

using System.Windows.Forms;

using System.Data;

using System.Drawing;

using System.ComponentModel;

using System.Drawing.Design;

using System.Reflection;

namespace Framework.Forms

{

/// <summary>

///

/// </summary>

public class DataGridButtonColumn : System.Windows.Forms.DataGridColumnStyle

{

private Button button=new Button();

private bool editing = false;

private String buttonText;

private Int32 buttonWidth;

#region Proprietati

[DefaultValue("")]

[Bindable(true)]

public String ButtonText

{

get {return buttonText;}

set {buttonText=value;}

}

public override PropertyDescriptor PropertyDescriptor

{

get

{

return GetFirstPropertyDescriptor();

}

set

{

base.PropertyDescriptor=value;

}

}

#endregion

public DataGridButtonColumn()

{

button.BackColor=Color.FromName("Control");

button.FlatStyle=FlatStyle.System;

button.Click+=new EventHandler(OnButtonClick);

button.Visible=false;

}

protected void OnButtonClick(Object source,EventArgs args)

{

((DionGrid)DataGridTableStyle.DataGrid).OnItemComm and(new
DataGridCommandArgs(buttonText,

DataGridTableStyle.DataGrid.CurrentRowIndex));

}

private PropertyDescriptor GetFirstPropertyDescriptor()

{

if ( !DesignMode )

{

BindingManagerBase bmb;

DataGrid dg;

dg=DataGridTableStyle.DataGrid;

if ( dg.BindingContext!=null )

{

bmb=dg.BindingContext[dg.DataSource,dg.DataMember];

if ( bmb.GetItemProperties()!=null )

{

return bmb.GetItemProperties()[0];

}

}

}

return null;

}

protected override void Abort(int row)

{

RollBack();

HideButton();

EndEdit();

}

protected override bool Commit(CurrencyManager cm, int row)

{

HideButton();

if (!editing)

{

return true;

}

EndEdit();

return true;

}

protected override void ConcedeFocus()

{

button.Visible=false;

}

protected override void Edit(CurrencyManager cm, int row, Rectangle rect,
bool readOnly, string text, bool visible)

{

Rectangle bounds;

bounds=new
Rectangle(rect.X+(rect.Width-buttonWidth)/2,rect.Y,buttonWidth,rect.Height);

bounds.Inflate(0,-1);

button.Bounds=bounds;

button.Text=buttonText;

button.Visible=true;

if ( ReadOnly )

{

button.Enabled=false;

}

editing = true;

}

protected override int GetMinimumHeight()

{

return 10;

}

protected override int GetPreferredHeight(Graphics g, object o)

{

return 0;

}

protected override Size GetPreferredSize(Graphics g, object o)

{

return new Size(0, 0);

}

protected override void Paint(Graphics g, Rectangle rect, CurrencyManager
cm, int row)

{

Paint(g, rect, cm, row, false);

}

protected override void Paint(Graphics g, Rectangle rect, CurrencyManager
cm, int row, bool alignToRight)

{

string text = GetText(GetColumnValueAtRow(cm, row));

PaintText(g, rect, text, alignToRight);

}

protected override void Paint(Graphics g, Rectangle rect, CurrencyManager
cm, int row, Brush backBrush, Brush foreBrush, bool alignToRight)

{

string text = GetText(GetColumnValueAtRow(cm, row));

PaintText(g, rect, text, backBrush, foreBrush, alignToRight);

if ( editing && row.Equals(DataGridTableStyle.DataGrid.CurrentRowI ndex) )

{

g.FillRectangle(new
SolidBrush(DataGridTableStyle.DataGrid.SelectionBa ckColor), rect);

}

}

protected override void SetDataGridInColumn(DataGrid dg)

{

base.SetDataGridInColumn(dg);

if (button.Parent != dg)

{

if (button.Parent != null)

{

button.Parent.Controls.Remove(button);

}

}

if (dg != null)

{

dg.Controls.Add(button);

}

}

private int DataGridTableGridLineWidth

{

get

{

return (DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid)

? 1 : 0;

}

}

public void EndEdit()

{

editing = false;

Invalidate();

}

private string GetText(object o)

{

if (o == System.DBNull.Value)

{

return NullText;

}

if (o != null)

{

return o.ToString();

}

else

{

return string.Empty;

}

}

private void HideButton()

{

button.Visible = false;

if (button.Focused)

{

DataGridTableStyle.DataGrid.Focus();

}

}

private void RollBack()

{

}

protected virtual void PaintText(Graphics g, Rectangle rect, string

text, bool alignToRight)

{

Brush backBrush = new SolidBrush(DataGridTableStyle.BackColor);

Brush foreBrush = new SolidBrush(DataGridTableStyle.ForeColor);

PaintText(g, rect, text, backBrush, foreBrush, alignToRight);

}

protected virtual void PaintText(Graphics g, Rectangle rect, string text,
Brush backBrush, Brush foreBrush, bool alignToRight)

{

StringFormat format = new StringFormat();

if (alignToRight)

{

format.FormatFlags = StringFormatFlags.DirectionRightToLeft;

}

g.FillRectangle(backBrush, rect);

format.FormatFlags = StringFormatFlags.NoWrap;

//desenam buton

SizeF size;

size=g.MeasureString(buttonText,button.Font);

buttonWidth=(int)(size.Width*1.5);

Rectangle bounds;

bounds=new
Rectangle(rect.X+(rect.Width-buttonWidth)/2,rect.Y,buttonWidth,rect.Height);

bounds.Inflate(0,-1);

ControlPaint.DrawButton(g,bounds,ButtonState.Norma l);

format.Alignment=StringAlignment.Center;

format.LineAlignment=StringAlignment.Center;

g.DrawString(buttonText,button.Font,new
SolidBrush(button.ForeColor),bounds,format);

format.Dispose();

}

}

}

Hope this helps
Dan Cimpoiesu

"Zürcher See" <aq****@cannabismail.com> wrote in message
news:10***************@fuchs.cyberlink.ch...
Someone has implemented a Datagrid Button for the Windows.Form?

Nov 15 '05 #2
Thank's, i will look at it

"Dan Cimpoiesu" <da**********@gmx.net> schrieb im Newsbeitrag
news:u0****************@TK2MSFTNGP10.phx.gbl...
It's pretty complicated to implement custom datagridcolumnstyles that are
unbound to data.

This is a DataGridButtonColumn prgrammed by me.

using System;

using System.Collections;

using System.Windows.Forms;

using System.Data;

using System.Drawing;

using System.ComponentModel;

using System.Drawing.Design;

using System.Reflection;

namespace Framework.Forms

{

/// <summary>

///

/// </summary>

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

private Button button=new Button();

private bool editing = false;

private String buttonText;

private Int32 buttonWidth;

#region Proprietati

[DefaultValue("")]

[Bindable(true)]

public String ButtonText

{

get {return buttonText;}

set {buttonText=value;}

}

public override PropertyDescriptor PropertyDescriptor

{

get

{

return GetFirstPropertyDescriptor();

}

set

{

base.PropertyDescriptor=value;

}

}

#endregion

public DataGridButtonColumn()

{

button.BackColor=Color.FromName("Control");

button.FlatStyle=FlatStyle.System;

button.Click+=new EventHandler(OnButtonClick);

button.Visible=false;

}

protected void OnButtonClick(Object source,EventArgs args)

{

((DionGrid)DataGridTableStyle.DataGrid).OnItemComm and(new
DataGridCommandArgs(buttonText,

DataGridTableStyle.DataGrid.CurrentRowIndex));

}

private PropertyDescriptor GetFirstPropertyDescriptor()

{

if ( !DesignMode )

{

BindingManagerBase bmb;

DataGrid dg;

dg=DataGridTableStyle.DataGrid;

if ( dg.BindingContext!=null )

{

bmb=dg.BindingContext[dg.DataSource,dg.DataMember];

if ( bmb.GetItemProperties()!=null )

{

return bmb.GetItemProperties()[0];

}

}

}

return null;

}

protected override void Abort(int row)

{

RollBack();

HideButton();

EndEdit();

}

protected override bool Commit(CurrencyManager cm, int row)

{

HideButton();

if (!editing)

{

return true;

}

EndEdit();

return true;

}

protected override void ConcedeFocus()

{

button.Visible=false;

}

protected override void Edit(CurrencyManager cm, int row, Rectangle rect,
bool readOnly, string text, bool visible)

{

Rectangle bounds;

bounds=new
Rectangle(rect.X+(rect.Width-buttonWidth)/2,rect.Y,buttonWidth,rect.Height);
bounds.Inflate(0,-1);

button.Bounds=bounds;

button.Text=buttonText;

button.Visible=true;

if ( ReadOnly )

{

button.Enabled=false;

}

editing = true;

}

protected override int GetMinimumHeight()

{

return 10;

}

protected override int GetPreferredHeight(Graphics g, object o)

{

return 0;

}

protected override Size GetPreferredSize(Graphics g, object o)

{

return new Size(0, 0);

}

protected override void Paint(Graphics g, Rectangle rect, CurrencyManager
cm, int row)

{

Paint(g, rect, cm, row, false);

}

protected override void Paint(Graphics g, Rectangle rect, CurrencyManager
cm, int row, bool alignToRight)

{

string text = GetText(GetColumnValueAtRow(cm, row));

PaintText(g, rect, text, alignToRight);

}

protected override void Paint(Graphics g, Rectangle rect, CurrencyManager
cm, int row, Brush backBrush, Brush foreBrush, bool alignToRight)

{

string text = GetText(GetColumnValueAtRow(cm, row));

PaintText(g, rect, text, backBrush, foreBrush, alignToRight);

if ( editing && row.Equals(DataGridTableStyle.DataGrid.CurrentRowI ndex) )

{

g.FillRectangle(new
SolidBrush(DataGridTableStyle.DataGrid.SelectionBa ckColor), rect);

}

}

protected override void SetDataGridInColumn(DataGrid dg)

{

base.SetDataGridInColumn(dg);

if (button.Parent != dg)

{

if (button.Parent != null)

{

button.Parent.Controls.Remove(button);

}

}

if (dg != null)

{

dg.Controls.Add(button);

}

}

private int DataGridTableGridLineWidth

{

get

{

return (DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid)

? 1 : 0;

}

}

public void EndEdit()

{

editing = false;

Invalidate();

}

private string GetText(object o)

{

if (o == System.DBNull.Value)

{

return NullText;

}

if (o != null)

{

return o.ToString();

}

else

{

return string.Empty;

}

}

private void HideButton()

{

button.Visible = false;

if (button.Focused)

{

DataGridTableStyle.DataGrid.Focus();

}

}

private void RollBack()

{

}

protected virtual void PaintText(Graphics g, Rectangle rect, string

text, bool alignToRight)

{

Brush backBrush = new SolidBrush(DataGridTableStyle.BackColor);

Brush foreBrush = new SolidBrush(DataGridTableStyle.ForeColor);

PaintText(g, rect, text, backBrush, foreBrush, alignToRight);

}

protected virtual void PaintText(Graphics g, Rectangle rect, string text,
Brush backBrush, Brush foreBrush, bool alignToRight)

{

StringFormat format = new StringFormat();

if (alignToRight)

{

format.FormatFlags = StringFormatFlags.DirectionRightToLeft;

}

g.FillRectangle(backBrush, rect);

format.FormatFlags = StringFormatFlags.NoWrap;

//desenam buton

SizeF size;

size=g.MeasureString(buttonText,button.Font);

buttonWidth=(int)(size.Width*1.5);

Rectangle bounds;

bounds=new
Rectangle(rect.X+(rect.Width-buttonWidth)/2,rect.Y,buttonWidth,rect.Height);
bounds.Inflate(0,-1);

ControlPaint.DrawButton(g,bounds,ButtonState.Norma l);

format.Alignment=StringAlignment.Center;

format.LineAlignment=StringAlignment.Center;

g.DrawString(buttonText,button.Font,new
SolidBrush(button.ForeColor),bounds,format);

format.Dispose();

}

}

}

Hope this helps
Dan Cimpoiesu

"Zürcher See" <aq****@cannabismail.com> wrote in message
news:10***************@fuchs.cyberlink.ch...
Someone has implemented a Datagrid Button for the Windows.Form?


Nov 15 '05 #3
Hi,
Maybe you'll interested in this:
http://www.i-syn.gmxhome.de/devcom/colstyles/intro.htm

pcPirate
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by vinayak | last post: by
reply views Thread by Curtis Hatter | last post: by
7 posts views Thread by A.M | last post: by
3 posts views Thread by Jim Heavey | last post: by
5 posts views Thread by Jeff User | last post: by
9 posts views Thread by rn5a | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.