Home Forums WinForms controls Xceed Grid for WinForms Deletion Button for row

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14340 |

    Hi,

    is it possible to add a column to the Grid that contains some buttons like deletion buttons for example?

    I saw the DataBinding Example that deletes rows on key press of “Delete”. I can also use this but I also would like an addition delete button.

    Thx.

    Sebastian

    Imported from legacy forums. Posted by sebastian (had 3407 views)

    Odi [Xceed]
    Spectator
    Post count: 426

    I believe support@xceedsoft.com has a sample on how to do that – its not up in the KB yet, though.

    Odi

    Imported from legacy forums. Posted by Odi [Xceed] (had 256 views)

    User (Old forums)
    Member
    Post count: 23064

    The way I have done it is to create my own cell, override the painting and draw a button. When the cell is clicked just delete the row. It may be a bit extreme, but I can then add multiple buttons into the same cell to perform other jobs, edit and add sub row for example.

    Imported from legacy forums. Posted by Chris (had 357 views)

    User (Old forums)
    Member
    Post count: 23064

    Could you give a code sample?

    Imported from legacy forums. Posted by sebastian (had 361 views)

    User (Old forums)
    Member
    Post count: 23064

    Below is the code for the button cell itself. I’ve written it in .NET 2.0 there is a better way to draw the button as well with the button renderer but you get the picture.

    namespace Example
    {

    /// <summary>
    /// Cell that contains a button
    /// </summary>
    /// <remarks>Draws a button in a cell that can be clicked</remarks>
    internal class ButtonCell : Xceed.Grid.DataCell
    {

    #region ” Fields “

    /// <summary>
    /// The text that will be drawn on the cell
    /// </summary>
    private string m_buttonText = string.Empty;

    #endregion

    #region ” Constructor “

    /// <summary>
    /// Create the new filter concat cell row
    /// </summary>
    public ButtonCell()
    : base()
    { }

    /// <summary>
    /// Create an new button cell based on our template cell
    /// </summary>
    /// <param name=”template”></param>
    protected ButtonCell(ButtonCell templateCell)
    : base(templateCell)
    { }

    /// <summary>
    /// Create a new cell from the parent column.
    /// </summary>
    /// <param name=”parentColumn”>The parent column to create from</param>
    public ButtonCell(Xceed.Grid.Column parentColumn)
    : base(parentColumn)
    { }

    #endregion

    #region ” Properties “

    /// <summary>
    /// The text that wil be dispalyed in the button in this cell
    /// </summary>
    public string ButtonText
    {
    get { return m_buttonText; }
    set
    {
    if(null == value)
    { m_buttonText = string.Empty; }
    else
    { m_buttonText = value; }
    }
    }

    #endregion

    #region ” Methods “

    /// <summary>
    /// Create an instance of a button cell that will be added to a grid
    /// </summary>
    /// <returns></returns>
    protected override Xceed.Grid.Cell CreateInstance()
    {
    return new ButtonCell(this);
    }

    /// <summary>
    /// Draws the button normally so the user isn’t over it
    /// </summary>
    /// <param name=”g”>The graphics to draw with</param>
    /// <param name=”buttonArea”>The area to draw</param>
    /// <param name=”buttonText”>The text to draw on the button</param>
    private void DrawButtonNormal(System.Drawing.Graphics g)
    {

    // If visual styles supported then draw a better looking button
    if(System.Windows.Forms.Application.RenderWithStyles)
    {

    System.Windows.Forms.VisualStyles.VisualStyleRenderer buttonDraw = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Normal);
    buttonDraw.DrawBackground(g, DisplayRectangle);

    }
    else
    {

    System.Windows.Forms.ControlPaint.DrawButton(g, DisplayRectangle, System.Windows.Forms.ButtonState.Flat);

    }
    DrawString(g, false);

    }

    /// <summary>
    /// Draws a button when the mouse is hovering over it
    /// </summary>
    /// <param name=”g”>The graphics to draw with</param>
    /// <param name=”buttonArea”>The button area</param>
    /// <param name=”buttonText”>The text to draw on the button</param>
    private void DrawButtonHot(System.Drawing.Graphics g)
    {

    // If visual styles supported then draw!
    if(System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsSupported)
    {

    System.Windows.Forms.VisualStyles.VisualStyleRenderer buttonDraw = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Hot);
    buttonDraw.DrawBackground(g, DisplayRectangle);

    }
    else
    {

    System.Windows.Forms.ControlPaint.DrawButton(g, DisplayRectangle, System.Windows.Forms.ButtonState.Normal);

    }

    DrawString(g, false);

    }

    /// <summary>
    /// Draws a button when the user is pressing it
    /// </summary>
    /// <param name=”g”>The graphics to draw with</param>
    /// <param name=”buttonArea”>The button area</param>
    /// <param name=”buttonText”>The text to draw on the button</param>
    private void DrawButtonPressed(System.Drawing.Graphics g)
    {

    // If visual styles supported then draw!
    if(System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsSupported)
    {

    System.Windows.Forms.VisualStyles.VisualStyleRenderer buttonDraw = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Pressed);
    buttonDraw.DrawBackground(g, DisplayRectangle);

    }
    else
    {

    System.Windows.Forms.ControlPaint.DrawButton(g, DisplayRectangle, System.Windows.Forms.ButtonState.Pushed);

    }

    DrawString(g, true);

    }

    /// <summary>
    /// Draws the button string. If this is the last row in the grid the text
    /// should be Add otherwise remove
    /// </summary>
    /// <param name=”g”>The graphics to draw with</param>
    /// <param name=”textArea”>The area to draw in</param>
    /// <param name=”text”>The text to draw</param>
    /// <param name=”isPressed”>If the button is pressed then move the text slightly</param>
    private void DrawString(System.Drawing.Graphics g, bool isPressed)
    {

    System.Drawing.StringFormat format = new System.Drawing.StringFormat();
    format.Alignment = System.Drawing.StringAlignment.Center;
    format.LineAlignment = System.Drawing.StringAlignment.Center;
    System.Drawing.Rectangle textArea = DisplayRectangle;
    if(isPressed)
    textArea.Offset(1, 1); SDJXCEED@20

    Imported from legacy forums. Posted by Chris (had 5113 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.