473,245 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

Datagrid Button

Someone has implemented a Datagrid Button for the Windows.Form?
Nov 15 '05 #1
3 6049
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
0
by: Shane O. Pinnell | last post by:
I am sure this has come up before, but I haven't been able to find an answer as of yet. That said, any help is definitely appreciated! I have a datagrid populated from a dataset. I have a...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
0
by: Curtis Hatter | last post by:
I apologize if this has been answered, but on the .NET 247 site I saw a thread titled "Datagrid's ItemCommand Event", and had similar troubles with the Datagrid not properly firing the delete event...
7
by: A.M | last post by:
Hi, I want to refresh my DataGrid data on every postback. I have following code in Page_Load event, but after first page load, the DataGrid never gets refresh. Here is the code i have in...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls...
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
9
by: rn5a | last post by:
A DataGrid is populated with the records existing in a database. Each of the row in this DataGrid has a ButtonColumn. Assume that the DataGrid displays 10 records (i.e. 10 DataGridItems/rows). Each...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.