none
Allow or deny the user to create a new row in a data grid programmatically RRS feed

  • Question

  • Hi!

    In the properties window for a data grid control there is a checkbox called ""Neue Zeile hinzufügen" anzeigen" in the german version of Visual Studio LightSwitch. I would translate this to "show "add new row"" but I am not sure how this checkbox is really called in the english version. Anyway, if this checkbox is checked, the user is able to add new rows and vice versa. Now I want to allow the user to add new rows programmatically with an instruction such as this:

    FindControl("<MyDataGrid>").SetProperty("<?>", <boolean result>)

    Unfortunately I don't know the property name (<?>). Can anybody help?

    How can I find out which properties a control actually has?

    Thursday, November 17, 2011 3:56 PM

Answers

  • It'd be a little more complicated than just using FindControl, that only gives you a reference to an IContentItemProxy, or actually maybe it would work using SetProperty, I haven't tried that come to think of it.

    BUT, the problem is, that although a WPF DataGrid (System.Windows.Controls) has a CanUserAddRows property, it would appear that the Silverlight DataGrid (System.Windows.Controls.Data) does not.

    And yet the LS team have somehow managed to do it anyway.


    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by FaDecto Friday, November 18, 2011 11:58 AM
    Friday, November 18, 2011 9:31 AM
    Moderator

All replies

  • Hi

    You can enable or disable Add Button (+) in Grid's Command Bar

     

    partial void <Screen>_InitializeDataWorkspace(List<IDataService> saveChangesTo)
    {
    	if (<Condition>)
    	{
    		this.FindControl("VisitsCollectionAddAndEditNew").IsEnabled = false;
    	}
    }
    

    Neue Zeile hinzufügen anzeigen = Show Add-new Row

    Hope this help


    Spaso Lazarevic

    • Edited by Spaso Lazarevic Thursday, November 17, 2011 5:25 PM Show Add-new Row
    Thursday, November 17, 2011 4:49 PM
  • Or

    if you don't want to show Add button in Command Bar, you can use IsVisible property.

    Best regards


    Spaso Lazarevic
    Thursday, November 17, 2011 5:18 PM
  • Hi Spaso!

    Thanks for your answer. But I dont have any add button in the command bar of my data grid.

       

    You can add a new row without an add button if the checkbox that I have marked is checked.

    If I would use "IsEnabled = False" for my data grid then I would not be able to scroll anymore. Besides the content would be greyed out which really sucks. IsVisible would not make any sense for my dara grid. Thats why I think I need to use SetProperty. But I don't know the name of that property.

    Thursday, November 17, 2011 5:58 PM
  • Sorry,

    but I think that there is no way that you can programatically  change "Show Add-new row" property.

    You need to set it to false and work with Add new item (+ sign).


    Spaso Lazarevic
    Thursday, November 17, 2011 6:18 PM
  • Sounds like you deleted the Add button on the grid somehow.  Add the button back.  Or remove the grid and add it back to start with the defaults again.

    Create another screen if needed to see what the defaults look like again.

    Thursday, November 17, 2011 6:38 PM
  • Actually I deleted the add button because I wanted to give the user only one option to add new rows. At that time I did not notice that checkbox (which is checked by default) in the properties window of a data grid. In my opinion, in terms of "user experience" it is better just to enter new data in the last row (like in excel) than to click an add button.

     

     

     

    All I need to know is the "propertyName As String", right? So why should there be no possibility to set such a property programmatically? Is it a limitation of silverlight?

    Friday, November 18, 2011 9:01 AM
  • Personally, I preffer use Add button and modal dialog than inline editing.

    Yes, to find control on the Screen you need to know property name (look at the properties window, Name text box).

    Property that you want is not expose to us (Show Add-new Row) to use it.

    You can always add button using Add functionallity from Command Bar:

    Best regards


    Spaso Lazarevic
    Friday, November 18, 2011 9:22 AM
  • It'd be a little more complicated than just using FindControl, that only gives you a reference to an IContentItemProxy, or actually maybe it would work using SetProperty, I haven't tried that come to think of it.

    BUT, the problem is, that although a WPF DataGrid (System.Windows.Controls) has a CanUserAddRows property, it would appear that the Silverlight DataGrid (System.Windows.Controls.Data) does not.

    And yet the LS team have somehow managed to do it anyway.


    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by FaDecto Friday, November 18, 2011 11:58 AM
    Friday, November 18, 2011 9:31 AM
    Moderator
  • I know this post is a little old, but it took me awhile to figure this out. My solution for this problem is below in case anyone else runs into the same issue. You need to cast the item source of your datagrid to an iEditableCollectionView then set "NewItemplaceholderPosition" to none.

    partial void <Screen>_Created()
    {
        this.FindControl(MyGrid).ControlAvailable += this.FormatGrid;
    }
    public void FormatGrid(object sender, Microsoft.LightSwitch.Presentation.ControlAvailableEventArgs e)
    {
         DataGrid eGrid = e.Control as DataGrid;
     
          ((System.ComponentModel.IEditableCollectionView)eGrid.ItemsSource).NewItemPlaceholderPosition = System.ComponentModel.NewItemPlaceholderPosition.None;
    }

    • Proposed as answer by KKalik Wednesday, January 30, 2013 11:25 PM
    Wednesday, January 30, 2013 11:25 PM
  • Thanks KKalik it works great.
    Thursday, October 23, 2014 2:44 PM