473,763 Members | 9,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid Button

Someone has implemented a Datagrid Button for the Windows.Form?
Nov 15 '05 #1
3 6081
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?

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

"Dan Cimpoiesu" <da**********@g mx.net> schrieb im Newsbeitrag
news:u0******** ********@TK2MSF TNGP10.phx.gbl. ..
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?


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
7932
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 header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the same control. Is there anyone who has tried this before and knows why this is hapenning ??
0
4793
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 TemplateColumn with a DropDownList (DDL) in the FooterItemTemplate that is populated in the Page_Load event using a Sub Routine as the DataSource of the DDL. The FooterItemTemplate's DDL populates as expected when the DataGrid's DataBind method is called. ...
3
4928
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 are Web Control & not HTML control. One of these buttons whose title is Delete is added on the aspx page in design view & also I double clicked on this button in design view to get the onclick code for this button in the code behind page. & for...
0
317
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 if using a push button. I could not find the thread here, so I'm posting my finding's here in hopes that it will help others that are having the same problem. The problem: In a DataGrid the "Delete" command does not fire properly from a...
7
3555
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 Page_load (it works at first page_load): Dim DS As DataSet Dim MyConnection As System.Data.SqlClient.SqlConnection
3
4885
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 the best method? Do you have a sample of how to do this?
1
4338
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 (watch the layout, some have child controls):
5
6703
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 missing here, but after 2 days I am ready to explode ! Please help. To create the Delete button I selected the grid in design view, clicked the "Columns" property and added the Delete button in the
9
5011
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 row in the DataGrid (of course, except the Header & the Footer) is accompanied by a ButtonColumn i.e. there are 10 Buttons in the DataGrid. The Buttons that get rendered by the ButtonColumns - how do I get the ID of each of these Buttons?
0
9383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9933
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8819
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3515
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.