Saving Flexgrid with vb.net

Posted by: travis on 10 July 2019, 8:24 am EST

  • Posted 10 July 2019, 8:24 am EST

    I’ve got a Flexgrid that I’m trying to get to save. I’ve added it like below, and my Controller has ActionResults for GridUpdate/Create/Delete, but it doesn’t ever seem to get there. What am I missing?

    
    @(Html.C1().FlexGrid(Of OrderDetail)() _
                                                    .Id("flexGrid") _
                                                    .AutoGenerateColumns(False) _
                                                    .Bind(Function(ib) ib.Bind(Model.Details).Update(Url.Action("GridUpdate")).Create(Url.Action("GridCreate")).Delete(Url.Action("GridDelete"))) _
                                                    .IsReadOnly(False) _
                                                    .CssClass("grid") _
                                                    .Columns(Sub(bl)
                                                                 bl.Add(Sub(cb) cb.Binding("OrderDetailID").Header("OrderDetailID").Width("0.4*").IsReadOnly(True))
                                                                 bl.Add(Sub(cb) cb.Binding("Width").Header("Width").Width("*").Name("Width"))
                                                                 bl.Add(Sub(cb) cb.Binding("Height").Header("Height").Width("*").Name("Height"))
                                                                 bl.Add(Sub(cb) cb.Binding("Quantity").Header("Quantity").Width("*").Name("Quantity"))
                                                                 bl.Add(Sub(cb) cb.Binding("MatchingBack").Header("MatchingBack").Width("*").Name("MatchingBack"))
                                                                 bl.Add(Sub(cb) cb.Binding("SqFt").Header("SqFt").Width("*").IsReadOnly(True))
                                                                 bl.Add(Sub(cb) cb.Binding("TotalPrice").Header("Total").Width("*").IsReadOnly(True))
                                                             End Sub)
        )
    
    
  • Posted 10 July 2019, 6:00 pm EST

    Hi,

    The FlexGrid should update the existing record as per the provided code snippet. Could you please verify the if the Update is performing or not by adding the debug point at the starting of “GridUpdate” method in controller?

    If the debug point is hitting, it means update action is called and there is some code error while updating the Data.

    Also please set the AllowAddNew and AllowDelete to true for performing the add and delete operation on FlexGrid.

    In case the debug point is not hitting, please try after updating the Url action with Controller name as follows:

    @Url.Action("GridOrderUpdate", "Home")
    

    If the issue persists, please share a demo sample depicting your issue.

    Regards,

    Manish Gupta

  • Posted 11 July 2019, 1:24 am EST

    I’ve attached a sample. I don’t actually have any save code, I was just dropping debug points and confirming that it got there.

    There are 2 views for the default people model, home/PersonDetail and home/index. I’ve got them going to 2 differant updates, and it works in both cases. I’ve added the controller as the path as suggested as well.

    My model for order details never seems to go to the update and I can’t see what I’ve got different. I’m fairly new to the MVC Model, so I’m guessing I’m going something foolish.

    Thanks in advance.

    EditSample.zip

  • Posted 12 July 2019, 1:38 am EST

    Hello,

    Thanks for the sample. The issue is because of GUID type field. The CollectionViewEditRequest is type of Dynamic(T) which seems the bug from MS.

    Please refer to the following Stackoverflow:

    To fix this issue, we need to add

    https://stackoverflow.com/questions/19612325/stackoverflowexception-when-accessing-member-of-nested-class-via-a-dynamic-refer

    https://stackoverflow.com/questions/22672775/stackoverflowexception-when-accessing-member-of-generic-type-via-dynamic-net-c

    For this we need to write the GuidConverter for this and jsonconvert attribute. Please refer to the attached sample and following code snippet:

    Public Class GuidConverter
        Inherits JsonConverter
    
        Public Overrides Function CanConvert(ByVal objectType As Type) As Boolean
            Return objectType = GetType(Guid)
        End Function
    
        Protected Overrides Function ReadJson(ByVal reader As JsonReader, ByVal objectType As Type, ByVal existedValue As Object) As Object
            Try
                Dim value = reader.ReadValue(Of String)()
                Return New Guid(value)
            Catch __unusedException1__ As Exception
                Return existedValue
            End Try
        End Function
    
        Protected Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal value As Object)
            writer.WriteValue(If(value Is Nothing, Nothing, value.ToString()))
        End Sub
    End Class
    
    Public Class OrderDetail
        Public Sub New()
        End Sub
    
        Public Property ID As Integer
        <JsonConverter(GetType(GuidConverter))>
        Public Property OrderID As Guid
        Public Property Style As Integer
        Public Property Hand As String
        Public Property Height As Double
        Public Property Width As Double
        Public Property Quantity As Integer
        Public Property MatchingBack As Integer
        Public Property DrillForHinges As Integer
        Public Property Fingerpull As Integer
    
        Public ReadOnly Property MatchingBackFormatted As String
            Get
    
                If MatchingBack = 0 Then
                    Return "No"
                Else
                    Return "Yes"
                End If
            End Get
        End Property
    
        Public ReadOnly Property StyleName As String
            Get
    
                If MatchingBack = 0 Then
                    Return "No"
                Else
                    Return "Yes"
                End If
            End Get
        End Property
    
        Public ReadOnly Property SqFt As Double
            Get
                Return Height * Width
            End Get
        End Property
    
        Public ReadOnly Property TotalPrice As Double
            Get
                Return Height * Width * 6
            End Get
        End Property
    End Class
    
    

    Regards,

    Manish Gupta

    FlexGrid-UpdateCall-GUID.zip

  • Posted 17 July 2019, 3:13 am EST

    Thanks! That’s perfect. I didn’t even think about the GUID. I just retyped it as a string and I’ll deal with it behind the scenes.

    Any chance you know how to make it update the calculated fields (eg: the SQ Ft is calculated and it doesn’t update after the line changes.

  • Posted 17 July 2019, 11:48 pm EST

    Hello,

    To update the computed field, we can use one of the following method:

    1. Make Ajax call to get updated data on CellEditEnded or RowEditEnded event
    2. Update the value on CellEditEnded event at Client

      Please use the following code to update the value at client:
    function OnCellEditEnded(s,e){
    	if(s.columns[e.col].binding=="Width" || s.columns[e.col].binding=="Height"){
    		var width = s.getCellData(e.row,s.columns.getColumn("Width"),true);
    		var hieght = s.getCellData(e.row,s.columns.getColumn("Height"),true);
    		var sqFt= width*height;
    		s.setCellData(e.row,s.columns.getColumn("SqFt"),sqFt);
    	}
    }
    

    Hope it helps!

    Regards,

    Manish

  • Posted 18 July 2019, 7:46 am EST

    That helps a lot. Is it possible to directly read it back from the model though? In that example, yes, that works perfectly, but the price, for example, is a much more complicated formula and it would be a shame to duplicate it in more than one place.

  • Posted 18 July 2019, 7:49 pm EST

    Hello,

    We are sorry for the inconvenience. This is because the Data shown at Client to Convert which leads to this issue.

    In WinForms/WPF, the INotifyPropertyChanged is quite useful but this doesn’t work in MVC. Please refer to the following issue at stackoverflow:

    https://stackoverflow.com/questions/36922982/inotifypropertychanged-how-or-does-this-work-in-web-apps-such-as-mvc

    Regards,

    Manish Gupta

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels