473,324 Members | 2,473 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,324 software developers and data experts.

Column with buttons in DataGrid

Hi

I've been looking a way to put buttons in a column, but almost every example
is for WebForms and I need it for WinForms...

I found a way for doing it, with the DataGridColumnStyle class, but in my
example, the button is unique for the entire grid and it's visible only when
I click the field...

Any help is welcome...

tnx
Nov 16 '05 #1
2 5736
Will,

Using the DataGridColumnStyle is the right way to go. Can you post your
solution, so we can take a look at it? I'm a little confused by what you
are saying about the button being unique for the entire grid.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Will" <so********@hotmail.com> wrote in message
news:co**********@domitilla.aioe.org...
Hi

I've been looking a way to put buttons in a column, but almost every
example is for WebForms and I need it for WinForms...

I found a way for doing it, with the DataGridColumnStyle class, but in my
example, the button is unique for the entire grid and it's visible only
when I click the field...

Any help is welcome...

tnx

Nov 16 '05 #2
Ok, this is the Class that I downloaded, the problem is that the button
appears only when I click a cell in the column with this style,
otherwise it shows a normal cell (just text)...

using System;
using System.Windows.Forms;
using System.Drawing;

namespace ModuloTote
{
public class DataGridControlButtonColumn:DataGridColumnStyle
{
private DataGrid grid;
// a reference to the grid

private int minimumHeight;
// the minimum height of a row

private int rowNum = -1;
// indicates the row number that shows the button

public System.Windows.Forms.Button Button = null;
// a reference to the hosted button

//
// ---- Methods ----
//

public DataGridControlButtonColumn()
{
// Default constructor. Creates a new button
//
Helper.Trace( "Constructor()" );
this.Button = new System.Windows.Forms.Button();
this.Button.BackColor = System.Drawing.Color.Silver;
this.Button.Visible = false;
Button.Click += new EventHandler(Button_Click);
}

protected override void Abort( int rowNum )
{
Helper.Trace( "Abort()", rowNum );
this.Button.Visible = false;
this.rowNum = -1;
}

protected override bool Commit( CurrencyManager dataSource, int rowNum )
{
Helper.Trace( "Commit()", rowNum );
this.Button.Visible = false;
this.rowNum = -1;
return true;
}

protected override void Edit( CurrencyManager source, int rowNum,
Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible )
{
// Start editing
//
Helper.Trace( "Edit()", rowNum, instantText );
// retrieve the current value
object value = this.GetColumnValueAtRow( source, rowNum );
if ( value == null ) this.Button.Text = this.NullText;
else this.Button.Text = value.ToString();
// move the button over the current cell and show
this.Button.Bounds = bounds;
this.Button.Visible = true;
this.Button.Focus();
this.rowNum = rowNum;
}

protected override void ConcedeFocus()
{
Helper.Trace( "ConcedeFocus()" );
this.Button.Visible = false;
}

protected override int GetMinimumHeight()
{
// Helper.Trace( "GetMinimumHeight()", cm.Position, cm.Count );
return 10;
}

protected override int GetPreferredHeight( Graphics g, object value )
{
Helper.Trace( "GetPreferredHeight()", value );
return 10;
}

protected override Size GetPreferredSize( Graphics g, object value )
{
Helper.Trace( "GetPreferredSize()", value );
return new System.Drawing.Size( 10, 20 );
}

protected override void Paint( Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum )
{
this.Paint( g, bounds, source, rowNum, false );
}

protected override void Paint( Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum, bool alignToRight )
{
// Helper.Trace( "Paint()", rowNum );
// no need to draw anything if the button is visible
if ( this.rowNum == rowNum && this.Button.Visible ) return;
object value = this.GetColumnValueAtRow( source, rowNum );
string text;
if ( value == null || value == DBNull.Value ) text = this.NullText;
else text = value.ToString();
// clear the background
Brush bgBrush = new SolidBrush( this.DataGridTableStyle.BackColor );
Brush fgBrush= new SolidBrush( this.DataGridTableStyle.ForeColor );
g.FillRectangle( bgBrush, bounds );
// draw the string
g.DrawString( text, this.DataGridTableStyle.DataGrid.Font, fgBrush,
bounds );
}

protected override void SetDataGrid( DataGrid value )
{
// not called yet....
Helper.Trace( "SetDataGrid()", value );
this.grid = value;
}

protected override void SetDataGridInColumn( DataGrid grid )
{
// add the button to the controls collection of the grid
Helper.Trace( "SetDataGridInColumn()", grid );
if ( this.Button.Parent != grid )
{
if ( this.Button.Parent != null )
this.Button.Parent.Controls.Remove( this.Button );
if ( grid != null ) grid.Controls.Add( this.Button );
}
// store a reference to the grid and attach event mouse handlers
this.grid = grid;
}

private void Button_Click(object sender, EventArgs e)
{
MessageBox.Show(this.rowNum.ToString());
}
}
}
.... and the Helper class...
using System;

namespace ModuloTote
{
public class Helper
{
private Helper()
{
}

public static void Trace ( params object[] list )
{
DateTime now = DateTime.Now;
Console.Write( now.ToString( "mm:ss.fff " ));
for ( int i = 0; i < list.Length; i++ )
{
Console.Write( "{0} ", list[i] );
}
Console.WriteLine();
}
}
}
....Hope somebody can give me a clue...

tnx
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: AR | last post by:
I have a datagrid containing a single combobox column. It also contains an 'Add New Row' button at the bottom of the datagrid. The datagrid is in a form that contains 'Save' and 'Restore' buttons....
2
by: Raj | last post by:
Hi, When we are sorting the DataGrid Boolean column the grid is becoming redcross. I have my own PPMIPDataGridBoolColumn class inherited from System.Windows.Forms.DataGridBoolColumn. In this...
1
by: Javier Ros | last post by:
hi guys, I´m working with datagrids and use the button columns, but I don´t like the buttons prefers the imagebutton, how to put a image in the place of button? without change the HTML code,...
1
by: Hypo | last post by:
I want to add some JS attributes on datagrid's button column. I tried folowing: for (int i=0; i < DataGrid1.Controls.Count; i++) if (DataGrid1.Controls is...
1
by: Manu | last post by:
Is it possible to make a button on a DataGrid template column, Trigger the DataGrid "Selected Index change" event. Is it possible to trigger that event not through the button column ?
1
by: RN | last post by:
Hi. I used this article to add a template column to a datagrid that is entirely created with code: ...
0
by: Stephen Skinner | last post by:
Ok, I've banged my head on this as long as I care to. I'm trying to use image buttons to add nice graphical edit butons to my datagrid. The link buttons trigger the datagrid commands, but the image...
5
by: acool | last post by:
I have been trying to do this for hours with only limited success. I have no idea why MS makes something that should be so straight forward a real pain. Can anyone point me in the right direction?
2
by: neoret | last post by:
Hello. I need some help on how to add a radiobutton to every row in the first column in a datagrid. The datasource to the datagrid is a dataTable. I loop through this table to add rows and...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.