We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Insert value in cell

I've finally got the sfdatagrid working as I want except for one thing. How do I insert a value in a cell of a new row after the AddNewRowInitialising event has completed?

This is the scenario:
User adds new row. Initialising takes place.
User enters data in cells 1, 2 and 3.
On Cell 4, the user RIGHT clicks the cell to bring up a dialog search form to search for an item User selectes the item and the dialog closes. The code now has the data the user selected. How do I insert this data in Cell 4? Remember this is a new row.

8 Replies

FP Farjana Parveen Ayubb Syncfusion Team January 9, 2020 11:35 AM UTC

Hi Tom, 
Thank you for using Syncfusion controls. 
You can able to achieve your requirement by set the cell value for a add new row cell by getting the ItemPropertiesProvider in SfDataGrid.View. Please refer the below code snippet, 
sfDataGrid.View.GetPropertyAccessProvider().SetValue(sfDataGrid.GetRecordAtRowIndex(rowIndex), sfDataGrid.Columns["CustomerID"].MappingName, data); 
Farjana Parveen A

TK Tom Krieg January 10, 2020 01:45 AM UTC

Thank you for your response. Your suggestion came up with some errors, but I modified it to use parentheses '()' instead of square brackets and used
sfdatagrid.GetRecordEntryAtRowIndex(rowindex) rather than GetRecordAtRowIndex. Now I have no compilation errors and I'll run a test to see if it waorks as I want.

TK Tom Krieg January 10, 2020 02:11 AM UTC

Here is my code: Right click on a cell sets the cell value (THis is a new row)
It throws an error in the SetCellValue sub (single line) :
"Object Reference not set to an instance of an object"

   at Syncfusion.Data.PropertyAccessor.<>c__DisplayClass1.<InitializeSet>b__0(Object instance, Object[] parameters)
   at Syncfusion.Data.PropertyAccessor.SetValue(Object obj, Object value)
   at Syncfusion.Data.ItemPropertiesProvider.SetValue(Object record, String propName, Object value)
   at Syncfusion.WinForms.DataGrid.Data.DataGridItemsPropertiesProvider.SetValue(Object

Private Sub gridReceipts_CellClick(sender As Object, e As CellClickEventArgs) Handles gridReceipts.CellClick
        Dim FieldName As String
        Dim CellValue As String
        If e.MouseEventArgs.Button = MouseButtons.Right Then
            Dim rci As RowColumnIndex = gridReceipts.TableControl.PointToCellRowColumnIndex(e.MouseEventArgs.Location)
            Dim RowNum As Integer = rci.RowIndex - 1
            Dim ColNum As Integer = rci.ColumnIndex - 1
            e.Cancel = True
            FieldName = gridReceipts.Columns(ColNum).ToString
            Select Case FieldName
                Case "OrderNumber"
                    ' Display outstanding Purchase Orders for this supplier
                Case "OrderLine"
                    ' Display all the still-outstanding line items for the
                    ' order number (above)
                Case "PartNumber"
                    ' Display the Parts Catalogue for part number selection and search
                Case "ManufacturerCode"                   
                     ' Display all Manufacturers (Brands) for this part number
                     '   ********* This is the call to set cell value **************************
                     ' **************************************************************
                     SetCellValue(RowNum, "ManufacturerCode", "WHL")
                Case "UnitCost"
                    ' If no unit cost carried fwd from a Purchase Order, display the last
                    ' 20 receipts for this item and their cost
                Case "JobOrInvoiceNumber"
                    ' Display the Service Jobs or Customer Parts Orders as determined.
            End Select
            CellValue = GetCellValue(RowNum, ColNum)
        End If
    End Sub

Private Sub SetCellValue(ByVal rowindex As Integer, FieldName As String, ByVal data As String)
        gridReceipts.View.GetPropertyAccessProvider().SetValue(gridReceipts.GetRecordEntryAtRowIndex(rowindex), gridReceipts.Columns(FieldName).MappingName, data)
    End Sub

SS Susmitha Sundar Syncfusion Team January 10, 2020 02:24 PM UTC

Hi Tom, 

Thank you for your update. 

We can able to reproduce your reported issue. We suggest to use the GetRecordAtRowIndex() for AddNewRowIndex, but you are using GetRecordEntryAtRowIndex(). You cant use this method for AddNewRowIndex. So that, it throws an error.  
We have prepared the sample using your provided code snipet and replace the GetRecordEntryAtRowIndex() by GetRecordAtRowIndex() method. Its working fine. Please refer the below sample, 

Can you please explain the need of GetRecordEntryAtRowIndex() method?  It will be helpful for us to check on it and provide you the solution at the earliest.   
Susmitha S 

TK Tom Krieg January 11, 2020 02:31 AM UTC

When I use GetRecordAtRowIndex the compiler throws an error

'GetRecordAtRowIndex' is not a member of sfdatagrid.

The link you provided throws an error when I click it. "The credentials you signed in with are not valid for this download."

TK Tom Krieg January 11, 2020 03:06 AM UTC

OK. So now I've referenced Syncfusion.WinForms.DataGrid.Interactivity and the compiler no longer throws an error. I also discovered that if the cell is in edit mode, a mouse right-click just brings up the standard Windows context menu. I.e. the right mouse click is not trapped by my code which is bypassed. When the cell is not in edit, the  right mouse click works. When the right mouse click works, the value inserted in the cell is not visible until the cell goes into edit or until the user moves off the cell. So I have two issues:

Can the right mouse click also be trapped when the cell is in edit mode?

Can the inserted value be visible immediately?

Thank you for your help.

SS Susmitha Sundar Syncfusion Team January 13, 2020 05:21 PM UTC

Hi Tom, 
Thank you for your update. 
Query1: Can the right mouse click also be trapped when the cell is in edit mode? 
When you edit the SfDataGrid, editor right click raised. We can’t get the SfDataGrid right click. 
Query2: Can the inserted value be visible immediately? 
Currently, we are checking the possibilities to achieve your requirement, we will check and provide you details on January 14, 2020. 
We appreciate your patience until then. 
Susmitha S 

SS Susmitha Sundar Syncfusion Team January 14, 2020 12:43 PM UTC

Hi Tom, 
Thank you for your patience. 
When you click the AddNewRow, that row did not commit with SfDataGrid until you enter that row. So, its property changed not raised by SfDataGrid. If you want to commit that value immediately, you need to invalidate that row. Please refer the below code snippet, 
Imports Syncfusion.WinForms.Core.Utils 
Private Sub SfListView1_SelectionChanged(ByVal sender As Object, ByVal e As Syncfusion.WinForms.ListView.Events.ItemSelectionChangedEventArgs) 
       Dim data = (TryCast(sender, Syncfusion.WinForms.ListView.SfListView)).SelectedItem 
       Dim sfDataGrid = Me.dataGridForm.sfDataGrid1 
       If sfDataGrid.CurrentCell IsNot Nothing AndAlso sfDataGrid.CurrentCell.RowIndex >= 0 Then 
              Dim rowIndex = sfDataGrid.CurrentCell.RowIndex 
              sfDataGrid.View.GetPropertyAccessProvider().SetValue(sfDataGrid.GetRecordAtRowIndex(rowIndex), sfDataGrid.Columns("Country").MappingName, data) 
              Dim invalidateRowRectangle = ReflectionHelper.GetMethod(sfDataGrid.TableControl.GetType(), "InvalidateRowRectangle") 
              ReflectionHelper.Invoke(invalidateRowRectangle, sfDataGrid.TableControl, New Object() { rowIndex,False }) 
       End If 
Please let us know if you need further assistance on this. 
Susmitha S 

Live Chat Icon For mobile
Up arrow icon