Results 1 to 3 of 3

Thread: add button to datagrid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471

    add button to datagrid

    How can I add a column of buttons to datagrid?

  2. #2
    New Member
    Join Date
    Dec 2004
    Posts
    2

    Re: add button to datagrid

    Did you ever resolve this or does anyone else know how this can be acheived? I found plenty on how to achieve this with data grid web server control but nothing for a windows form.

    Any help much appreciated

    Cheers

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: add button to datagrid

    You can do this, but you have to create a class that inherits the DataGridColumnStyle class, here's an example:

    DataGridButtonColumn Class Definition:
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Windows.Forms
    4. Imports System.Drawing
    5. Imports System.ComponentModel
    6.  
    7. Public Class DataGridButtonColumn
    8.    Inherits DataGridColumnStyle
    9.  
    10.    Public Event ButtonClick As EventHandler
    11.  
    12.    ' Button to be embedded in the cell
    13.    Private m_button As New Button
    14.  
    15.    Private Const BUTTON_SIZE = 23
    16.  
    17.    Public Sub New()
    18.       ' Hide the button
    19.       m_button.Visible = False
    20.       m_button.Text = "..."
    21.       m_button.BackColor = Color.FromKnownColor(KnownColor.Control)
    22.       ' Add the Event Handlet to monitor Button Clicks
    23.       AddHandler m_button.Click, AddressOf OnButtonClick
    24.    End Sub
    25.  
    26.    Protected Overrides Sub Abort(ByVal rowNum As Integer)
    27.       ' Remove the button and redraw the cell
    28.       Invalidate()
    29.    End Sub
    30.  
    31.    Protected Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean
    32.       ' Edit is finishing...
    33.       ' Redraw the cell
    34.       Invalidate()
    35.       Return True
    36.    End Function
    37.  
    38.    Protected Overloads Overrides Sub Edit(ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
    39.       ' Initiate an Edit..
    40.  
    41.       ' If the cell is visible, setup the Button
    42.       If cellIsVisible Then
    43.          ' Position and Size the Button
    44.          m_button.Location = New Point(bounds.X + 2, bounds.Y + 2)
    45.          m_button.Size = New Size(bounds.Width - 4, bounds.Height - 4)
    46.          ' Make it visible
    47.          m_button.Visible = True
    48.       Else
    49.          ' Hide the Button
    50.          m_button.Visible = False
    51.       End If
    52.  
    53.       ' If the Button is visible, redraw the cell to show it
    54.       If m_button.Visible Then
    55.          DataGridTableStyle.DataGrid.Invalidate(bounds)
    56.       End If
    57.    End Sub
    58.  
    59.    ' Override the "Width" property to allow us to
    60.    ' resize the Button to fit the column
    61.    Public Overrides Property Width() As Integer
    62.       Get
    63.          Return MyBase.Width
    64.       End Get
    65.       Set(ByVal Value As Integer)
    66.          MyBase.Width = Value
    67.          m_button.Width = Value - 4
    68.       End Set
    69.    End Property
    70.  
    71.    Protected Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal value As Object) As Size
    72.       ' Return the default size the cell should be
    73.       Return New Size(BUTTON_SIZE, BUTTON_SIZE)
    74.    End Function
    75.  
    76.    Protected Overrides Function GetMinimumHeight() As Integer
    77.       ' Return the minimum height this column can be
    78.       Return BUTTON_SIZE
    79.    End Function
    80.  
    81.    Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer
    82.       ' Return the default/preferred height of this column
    83.       Return BUTTON_SIZE
    84.    End Function
    85.  
    86.    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
    87.       ' Call the overload
    88.       Paint(g, bounds, [source], rowNum, False)
    89.    End Sub
    90.  
    91.    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
    92.       ' Call the overload
    93.       Paint(g, bounds, [source], rowNum, Brushes.White, Brushes.Black, alignToRight)
    94.    End Sub
    95.  
    96.    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
    97.       ' Draw the cell to display the value
    98.       ' Get the current cell value
    99.       Dim value As Object = GetColumnValueAtRow([source], rowNum)
    100.       Dim rect As Rectangle = bounds
    101.  
    102.       ' Clear the cell
    103.       g.FillRectangle(backBrush, rect)
    104.    End Sub
    105.  
    106.    Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
    107.       ' Assign the Button to the specified Grid
    108.       MyBase.SetDataGridInColumn(value)
    109.       If Not (m_button.Parent Is Nothing) Then
    110.          m_button.Parent.Controls.Remove(m_button)
    111.       End If
    112.       If Not (value Is Nothing) Then
    113.          value.Controls.Add(m_button)
    114.       End If
    115.    End Sub
    116.  
    117.    Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    118.       ' Event handler for the Button
    119.       MyBase.ColumnStartedEditing(m_button)
    120.       RaiseEvent ButtonClick(Me, EventArgs.Empty)
    121.    End Sub
    122. End Class
    Example Usage:
    VB Code:
    1. Private Sub frmDataGridExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.       SetupSampleData()
    3.    End Sub
    4.  
    5.    Private Sub SetupSampleData()
    6.       ' Create a sample DataTable with Five empty rows
    7.       Dim table As New DataTable("Example")
    8.       table.Columns.Add("Column1", GetType(String))
    9.  
    10.       table.Rows.Add(New Object() {""})
    11.       table.Rows.Add(New Object() {""})
    12.       table.Rows.Add(New Object() {""})
    13.       table.Rows.Add(New Object() {""})
    14.       table.Rows.Add(New Object() {""})
    15.  
    16.       DataGrid1.DataSource = table
    17.  
    18.       ' Setup Table and Column Styles for the Grid
    19.       Dim ts As New DataGridTableStyle
    20.       Dim cs As New DataGridButtonColumn ' Use the new DataGridButtonColumn class
    21.  
    22.       ' Hook the ButtonClick event
    23.       AddHandler cs.ButtonClick, AddressOf GridButtonClick
    24.  
    25.       ' Map the styles to the Table and Column
    26.       ts.MappingName = "Example"
    27.       cs.MappingName = "Column1"
    28.       ts.GridColumnStyles.Add(cs)
    29.  
    30.       ' Apply the Styles
    31.       DataGrid1.TableStyles.Add(ts)
    32.    End Sub
    33.  
    34.    Private Sub GridButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    35.       ' Triggered when the button in the Grid is clicked.
    36.       MessageBox.Show("Button Clicked")
    37.    End Sub
    Regards,

    - Aaron.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width