Column with buttons in DataGrid

  • Thread starter Thread starter Will
  • Start date Start date
W

Will

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
 
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.
 
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 );
}
Console.WriteLine();
}
}
}


....Hope somebody can give me a clue...

tnx
 
Back
Top