Z
Zürcher See
Someone has implemented a Datagrid Button for the Windows.Form?
Dan Cimpoiesu said: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).OnItemCommand(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.CurrentRowIndex) )
{
g.FillRectangle(new
SolidBrush(DataGridTableStyle.DataGrid.SelectionBackColor), 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.Normal);
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 said:Someone has implemented a Datagrid Button for the Windows.Form?