How can I add a column of buttons to datagrid?
Printable View
How can I add a column of buttons 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
You can do this, but you have to create a class that inherits the DataGridColumnStyle class, here's an example:
DataGridButtonColumn Class Definition:Example Usage:VB Code:
Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.ComponentModel Public Class DataGridButtonColumn Inherits DataGridColumnStyle Public Event ButtonClick As EventHandler ' Button to be embedded in the cell Private m_button As New Button Private Const BUTTON_SIZE = 23 Public Sub New() ' Hide the button m_button.Visible = False m_button.Text = "..." m_button.BackColor = Color.FromKnownColor(KnownColor.Control) ' Add the Event Handlet to monitor Button Clicks AddHandler m_button.Click, AddressOf OnButtonClick End Sub Protected Overrides Sub Abort(ByVal rowNum As Integer) ' Remove the button and redraw the cell Invalidate() End Sub Protected Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean ' Edit is finishing... ' Redraw the cell Invalidate() Return True End Function 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) ' Initiate an Edit.. ' If the cell is visible, setup the Button If cellIsVisible Then ' Position and Size the Button m_button.Location = New Point(bounds.X + 2, bounds.Y + 2) m_button.Size = New Size(bounds.Width - 4, bounds.Height - 4) ' Make it visible m_button.Visible = True Else ' Hide the Button m_button.Visible = False End If ' If the Button is visible, redraw the cell to show it If m_button.Visible Then DataGridTableStyle.DataGrid.Invalidate(bounds) End If End Sub ' Override the "Width" property to allow us to ' resize the Button to fit the column Public Overrides Property Width() As Integer Get Return MyBase.Width End Get Set(ByVal Value As Integer) MyBase.Width = Value m_button.Width = Value - 4 End Set End Property Protected Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal value As Object) As Size ' Return the default size the cell should be Return New Size(BUTTON_SIZE, BUTTON_SIZE) End Function Protected Overrides Function GetMinimumHeight() As Integer ' Return the minimum height this column can be Return BUTTON_SIZE End Function Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer ' Return the default/preferred height of this column Return BUTTON_SIZE End Function Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer) ' Call the overload Paint(g, bounds, [source], rowNum, False) End Sub 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) ' Call the overload Paint(g, bounds, [source], rowNum, Brushes.White, Brushes.Black, alignToRight) End Sub 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) ' Draw the cell to display the value ' Get the current cell value Dim value As Object = GetColumnValueAtRow([source], rowNum) Dim rect As Rectangle = bounds ' Clear the cell g.FillRectangle(backBrush, rect) End Sub Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid) ' Assign the Button to the specified Grid MyBase.SetDataGridInColumn(value) If Not (m_button.Parent Is Nothing) Then m_button.Parent.Controls.Remove(m_button) End If If Not (value Is Nothing) Then value.Controls.Add(m_button) End If End Sub Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs) ' Event handler for the Button MyBase.ColumnStartedEditing(m_button) RaiseEvent ButtonClick(Me, EventArgs.Empty) End Sub End ClassRegards,VB Code:
Private Sub frmDataGridExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SetupSampleData() End Sub Private Sub SetupSampleData() ' Create a sample DataTable with Five empty rows Dim table As New DataTable("Example") table.Columns.Add("Column1", GetType(String)) table.Rows.Add(New Object() {""}) table.Rows.Add(New Object() {""}) table.Rows.Add(New Object() {""}) table.Rows.Add(New Object() {""}) table.Rows.Add(New Object() {""}) DataGrid1.DataSource = table ' Setup Table and Column Styles for the Grid Dim ts As New DataGridTableStyle Dim cs As New DataGridButtonColumn ' Use the new DataGridButtonColumn class ' Hook the ButtonClick event AddHandler cs.ButtonClick, AddressOf GridButtonClick ' Map the styles to the Table and Column ts.MappingName = "Example" cs.MappingName = "Column1" ts.GridColumnStyles.Add(cs) ' Apply the Styles DataGrid1.TableStyles.Add(ts) End Sub Private Sub GridButtonClick(ByVal sender As Object, ByVal e As EventArgs) ' Triggered when the button in the Grid is clicked. MessageBox.Show("Button Clicked") End Sub
- Aaron.