Someone has implemented a Datagrid Button for the Windows.Form?
Datagrid Button
Collapse
This topic is closed.
X
X
-
Zürcher SeeTags: None -
Dan Cimpoiesu
Re: Datagrid Button
It's pretty complicated to implement custom datagridcolumns tyles that are
unbound to data.
This is a DataGridButtonC olumn prgrammed by me.
using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;
using System.Drawing;
using System.Componen tModel;
using System.Drawing. Design;
using System.Reflecti on;
namespace Framework.Forms
{
/// <summary>
///
/// </summary>
public class DataGridButtonC olumn : System.Windows. Forms.DataGridC olumnStyle
{
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=val ue;}
}
public override PropertyDescrip tor PropertyDescrip tor
{
get
{
return GetFirstPropert yDescriptor();
}
set
{
base.PropertyDe scriptor=value;
}
}
#endregion
public DataGridButtonC olumn()
{
button.BackColo r=Color.FromNam e("Control");
button.FlatStyl e=FlatStyle.Sys tem;
button.Click+=n ew EventHandler(On ButtonClick);
button.Visible= false;
}
protected void OnButtonClick(O bject source,EventArg s args)
{
((DionGrid)Data GridTableStyle. DataGrid).OnIte mCommand(new
DataGridCommand Args(buttonText ,
DataGridTableSt yle.DataGrid.Cu rrentRowIndex)) ;
}
private PropertyDescrip tor GetFirstPropert yDescriptor()
{
if ( !DesignMode )
{
BindingManagerB ase bmb;
DataGrid dg;
dg=DataGridTabl eStyle.DataGrid ;
if ( dg.BindingConte xt!=null )
{
bmb=dg.BindingC ontext[dg.DataSource,d g.DataMember];
if ( bmb.GetItemProp erties()!=null )
{
return bmb.GetItemProp erties()[0];
}
}
}
return null;
}
protected override void Abort(int row)
{
RollBack();
HideButton();
EndEdit();
}
protected override bool Commit(Currency Manager cm, int row)
{
HideButton();
if (!editing)
{
return true;
}
EndEdit();
return true;
}
protected override void ConcedeFocus()
{
button.Visible= false;
}
protected override void Edit(CurrencyMa nager cm, int row, Rectangle rect,
bool readOnly, string text, bool visible)
{
Rectangle bounds;
bounds=new
Rectangle(rect. X+(rect.Width-buttonWidth)/2,rect.Y,button Width,rect.Heig ht);
bounds.Inflate( 0,-1);
button.Bounds=b ounds;
button.Text=but tonText;
button.Visible= true;
if ( ReadOnly )
{
button.Enabled= false;
}
editing = true;
}
protected override int GetMinimumHeigh t()
{
return 10;
}
protected override int GetPreferredHei ght(Graphics g, object o)
{
return 0;
}
protected override Size GetPreferredSiz e(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(GetColu mnValueAtRow(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(GetColu mnValueAtRow(cm , row));
PaintText(g, rect, text, backBrush, foreBrush, alignToRight);
if ( editing && row.Equals(Data GridTableStyle. DataGrid.Curren tRowIndex) )
{
g.FillRectangle (new
SolidBrush(Data GridTableStyle. DataGrid.Select ionBackColor), rect);
}
}
protected override void SetDataGridInCo lumn(DataGrid dg)
{
base.SetDataGri dInColumn(dg);
if (button.Parent != dg)
{
if (button.Parent != null)
{
button.Parent.C ontrols.Remove( button);
}
}
if (dg != null)
{
dg.Controls.Add (button);
}
}
private int DataGridTableGr idLineWidth
{
get
{
return (DataGridTableS tyle.GridLineSt yle == DataGridLineSty le.Solid)
? 1 : 0;
}
}
public void EndEdit()
{
editing = false;
Invalidate();
}
private string GetText(object o)
{
if (o == System.DBNull.V alue)
{
return NullText;
}
if (o != null)
{
return o.ToString();
}
else
{
return string.Empty;
}
}
private void HideButton()
{
button.Visible = false;
if (button.Focused )
{
DataGridTableSt yle.DataGrid.Fo cus();
}
}
private void RollBack()
{
}
protected virtual void PaintText(Graph ics g, Rectangle rect, string
text, bool alignToRight)
{
Brush backBrush = new SolidBrush(Data GridTableStyle. BackColor);
Brush foreBrush = new SolidBrush(Data GridTableStyle. ForeColor);
PaintText(g, rect, text, backBrush, foreBrush, alignToRight);
}
protected virtual void PaintText(Graph ics g, Rectangle rect, string text,
Brush backBrush, Brush foreBrush, bool alignToRight)
{
StringFormat format = new StringFormat();
if (alignToRight)
{
format.FormatFl ags = StringFormatFla gs.DirectionRig htToLeft;
}
g.FillRectangle (backBrush, rect);
format.FormatFl ags = StringFormatFla gs.NoWrap;
//desenam buton
SizeF size;
size=g.MeasureS tring(buttonTex t,button.Font);
buttonWidth=(in t)(size.Width*1 .5);
Rectangle bounds;
bounds=new
Rectangle(rect. X+(rect.Width-buttonWidth)/2,rect.Y,button Width,rect.Heig ht);
bounds.Inflate( 0,-1);
ControlPaint.Dr awButton(g,boun ds,ButtonState. Normal);
format.Alignmen t=StringAlignme nt.Center;
format.LineAlig nment=StringAli gnment.Center;
g.DrawString(bu ttonText,button .Font,new
SolidBrush(butt on.ForeColor),b ounds,format);
format.Dispose( );
}
}
}
Hope this helps
Dan Cimpoiesu
"Zürcher See" <aquila@cannabi smail.com> wrote in message
news:1068214430 .327299@fuchs.c yberlink.ch...[color=blue]
> Someone has implemented a Datagrid Button for the Windows.Form?
>
>[/color]
-
Zürcher See
Re: Datagrid Button
Thank's, i will look at it
"Dan Cimpoiesu" <dancimpoiesu@g mx.net> schrieb im Newsbeitrag
news:u0%23a8tTp DHA.2416@TK2MSF TNGP10.phx.gbl. ..[color=blue]
> It's pretty complicated to implement custom datagridcolumns tyles that are
> unbound to data.
>
> This is a DataGridButtonC olumn prgrammed by me.
>
> using System;
>
> using System.Collecti ons;
>
> using System.Windows. Forms;
>
> using System.Data;
>
> using System.Drawing;
>
> using System.Componen tModel;
>
> using System.Drawing. Design;
>
> using System.Reflecti on;
>
> namespace Framework.Forms
>
> {
>
> /// <summary>
>
> ///
>
> /// </summary>
>
> public class DataGridButtonC olumn :[/color]
System.Windows. Forms.DataGridC olumnStyle[color=blue]
>
> {
>
> 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=val ue;}
>
> }
>
> public override PropertyDescrip tor PropertyDescrip tor
>
> {
>
> get
>
> {
>
> return GetFirstPropert yDescriptor();
>
> }
>
> set
>
> {
>
> base.PropertyDe scriptor=value;
>
> }
>
> }
>
>
>
> #endregion
>
> public DataGridButtonC olumn()
>
> {
>
> button.BackColo r=Color.FromNam e("Control");
>
> button.FlatStyl e=FlatStyle.Sys tem;
>
> button.Click+=n ew EventHandler(On ButtonClick);
>
> button.Visible= false;
>
> }
>
> protected void OnButtonClick(O bject source,EventArg s args)
>
> {
>
> ((DionGrid)Data GridTableStyle. DataGrid).OnIte mCommand(new
> DataGridCommand Args(buttonText ,
>
> DataGridTableSt yle.DataGrid.Cu rrentRowIndex)) ;
>
> }
>
> private PropertyDescrip tor GetFirstPropert yDescriptor()
>
> {
>
> if ( !DesignMode )
>
> {
>
> BindingManagerB ase bmb;
>
> DataGrid dg;
>
> dg=DataGridTabl eStyle.DataGrid ;
>
> if ( dg.BindingConte xt!=null )
>
> {
>
> bmb=dg.BindingC ontext[dg.DataSource,d g.DataMember];
>
> if ( bmb.GetItemProp erties()!=null )
>
> {
>
> return bmb.GetItemProp erties()[0];
>
> }
>
> }
>
> }
>
> return null;
>
> }
>
> protected override void Abort(int row)
>
> {
>
> RollBack();
>
> HideButton();
>
> EndEdit();
>
> }
>
> protected override bool Commit(Currency Manager cm, int row)
>
> {
>
> HideButton();
>
> if (!editing)
>
> {
>
> return true;
>
> }
>
> EndEdit();
>
> return true;
>
> }
>
> protected override void ConcedeFocus()
>
> {
>
> button.Visible= false;
>
> }
>
> protected override void Edit(CurrencyMa nager cm, int row, Rectangle rect,
> bool readOnly, string text, bool visible)
>
> {
>
> Rectangle bounds;
>
> bounds=new
>[/color]
Rectangle(rect. X+(rect.Width-buttonWidth)/2,rect.Y,button Width,rect.Heig ht);[color=blue]
>
> bounds.Inflate( 0,-1);
>
> button.Bounds=b ounds;
>
> button.Text=but tonText;
>
> button.Visible= true;
>
> if ( ReadOnly )
>
> {
>
> button.Enabled= false;
>
> }
>
> editing = true;
>
> }
>
> protected override int GetMinimumHeigh t()
>
> {
>
> return 10;
>
> }
>
> protected override int GetPreferredHei ght(Graphics g, object o)
>
> {
>
> return 0;
>
> }
>
> protected override Size GetPreferredSiz e(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(GetColu mnValueAtRow(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(GetColu mnValueAtRow(cm , row));
>
> PaintText(g, rect, text, backBrush, foreBrush, alignToRight);
>
> if ( editing && row.Equals(Data GridTableStyle. DataGrid.Curren tRowIndex) )
>
> {
>
> g.FillRectangle (new
> SolidBrush(Data GridTableStyle. DataGrid.Select ionBackColor), rect);
>
> }
>
> }
>
> protected override void SetDataGridInCo lumn(DataGrid dg)
>
> {
>
> base.SetDataGri dInColumn(dg);
>
> if (button.Parent != dg)
>
> {
>
> if (button.Parent != null)
>
> {
>
> button.Parent.C ontrols.Remove( button);
>
> }
>
> }
>
> if (dg != null)
>
> {
>
> dg.Controls.Add (button);
>
> }
>
> }
>
> private int DataGridTableGr idLineWidth
>
> {
>
> get
>
> {
>
> return (DataGridTableS tyle.GridLineSt yle == DataGridLineSty le.Solid)
>
> ? 1 : 0;
>
> }
>
> }
>
> public void EndEdit()
>
> {
>
> editing = false;
>
> Invalidate();
>
> }
>
> private string GetText(object o)
>
> {
>
> if (o == System.DBNull.V alue)
>
> {
>
> return NullText;
>
> }
>
> if (o != null)
>
> {
>
> return o.ToString();
>
> }
>
> else
>
> {
>
> return string.Empty;
>
> }
>
> }
>
> private void HideButton()
>
> {
>
> button.Visible = false;
>
> if (button.Focused )
>
> {
>
> DataGridTableSt yle.DataGrid.Fo cus();
>
> }
>
> }
>
> private void RollBack()
>
> {
>
> }
>
> protected virtual void PaintText(Graph ics g, Rectangle rect, string
>
> text, bool alignToRight)
>
> {
>
> Brush backBrush = new SolidBrush(Data GridTableStyle. BackColor);
>
> Brush foreBrush = new SolidBrush(Data GridTableStyle. ForeColor);
>
> PaintText(g, rect, text, backBrush, foreBrush, alignToRight);
>
> }
>
> protected virtual void PaintText(Graph ics g, Rectangle rect, string text,
> Brush backBrush, Brush foreBrush, bool alignToRight)
>
> {
>
> StringFormat format = new StringFormat();
>
> if (alignToRight)
>
> {
>
> format.FormatFl ags = StringFormatFla gs.DirectionRig htToLeft;
>
> }
>
> g.FillRectangle (backBrush, rect);
>
> format.FormatFl ags = StringFormatFla gs.NoWrap;
>
> //desenam buton
>
> SizeF size;
>
> size=g.MeasureS tring(buttonTex t,button.Font);
>
> buttonWidth=(in t)(size.Width*1 .5);
>
> Rectangle bounds;
>
> bounds=new
>[/color]
Rectangle(rect. X+(rect.Width-buttonWidth)/2,rect.Y,button Width,rect.Heig ht);[color=blue]
>
> bounds.Inflate( 0,-1);
>
> ControlPaint.Dr awButton(g,boun ds,ButtonState. Normal);
>
> format.Alignmen t=StringAlignme nt.Center;
>
> format.LineAlig nment=StringAli gnment.Center;
>
> g.DrawString(bu ttonText,button .Font,new
> SolidBrush(butt on.ForeColor),b ounds,format);
>
> format.Dispose( );
>
> }
>
> }
>
> }
>
> Hope this helps
> Dan Cimpoiesu
>
> "Zürcher See" <aquila@cannabi smail.com> wrote in message
> news:1068214430 .327299@fuchs.c yberlink.ch...[color=green]
> > Someone has implemented a Datagrid Button for the Windows.Form?
> >
> >[/color]
>
>[/color]
Comment
-
pcPirate
Re: Datagrid Button
Hi,
Maybe you'll interested in this:
pcPirate
Comment
Comment