locked
Add a button to a DataGrid's row?

    Question

  • How can I add a button to a DataGrid's row? I see a command bar inside the DataGridRow, and I added a button there, but I am not seeing it when I run the application.

    Thanks!

    Tuesday, February 15, 2011 3:04 PM

Answers

All replies

  • I think this must be a bug in LS but perhaps someone can advise?

    In the absence of better solution, a possible way to do this is to handle the LoadingRow event on the datagrid and to manually add the button in code.

    The following code might point you in the right direction. Hope it helps!

     

    'you might need to add a reference in your client project to System.Windows.Controls.Data
    Private myGrid As DataGrid
    
    Private Sub EditablePersonGrid_Loaded()
    
      'grid is the name of my datagrid
      Dim proxy As IContentItemProxy = Me.FindControl("grid")
      myGrid = proxy.Control
    
      proxy.Invoke(Sub()
               Dim gd = TryCast(proxy.Control, System.Windows.Controls.DataGrid)
               AddHandler gd.LoadingRow, AddressOf grid_LoadingRow
             End Sub)
    End Sub
    
    Protected Sub grid_LoadingRow(ByVal sender As Object, ByVal e As DataGridRowEventArgs)
    
      Dim dgCell1 As DataGridCell
    
      'we're adding the button to the second cell (Columns(1)) so set as appropriate 
      dgCell1 = myGrid.Columns(1).GetCellContent(e.Row).Parent
    
      Dim btn1 As New Button
      btn1.Content = "ClickMe!"
      AddHandler btn1.Click, AddressOf btn_click
      dgCell1.Content = btn1
    
    End Sub
    
    Protected Sub btn_click()
      Me.ShowMessageBox("This is the code that runs when a button is clicked in the grid")
    End Sub
    
    

    Tuesday, February 15, 2011 5:26 PM
    • Marked as answer by juantar Wednesday, February 16, 2011 5:40 PM
    Wednesday, February 16, 2011 12:04 AM
  • I had buttons on a DataGrid working but took it out because it took too long to list with the contents of the row.  Seems like I just got lucky - as there seems to be some bugs.

    Currently, we just put a single button at the top of the DataGrid and just use the selected row within the button code.

    Wednesday, February 16, 2011 1:39 AM
  • How can I add a button to a DataGrid's row? I see a command bar inside the DataGridRow, and I added a button there, but I am not seeing it when I run the application.

    Thanks!


    This is an approach that is common in HTML style web site, but if you do it with LS and you have a lot of rows, you'll eat up a lot of resources for creating those buttons as objects. Not sure if it's worth it. Where as in HTML pages you don't have such issue.

    Just something to think about regarding resources.

    Wednesday, February 16, 2011 2:51 PM