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" <aq****@cannabi smail.com> wrote in message
news:10******** *******@fuchs.c yberlink.ch...
Someone has implemented a Datagrid Button for the Windows.Form?