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

Set value of a cell in new row of sfdatagrid

I'm still having trouble setting a cell value of a new row in a sfdatagrid. I've followed your code example, but still come up with a null reference exception. Could you please advise why your suggestion is not working? The User has clicked on the "Click here to create a line" prompt and sfdatagrid is empty (no existing rows). sfdatagrid is bound to an Observalbe Collection. The sfdatagrid is named "gridOrders". The grid consists of both hidden and visible columns. The first 3 columns are hidden. Here is the code:

    Private Sub gridorders_CurrentCellKeyPress(sender As Object, e As CurrentCellKeyPressEventArgs) Handles gridOrders.CurrentCellKeyPress        Dim CellValue As String = String.Empty
        Dim r As Integer = gridOrders.CurrentCell.RowIndex
        Dim c As Integer = gridOrders.CurrentCell.ColumnIndex
        Dim FieldName As String = gridOrders.CurrentCell.Column.ToString
        Dim EXTERNAL_NUMBER As String = String.Empty
        Select Case FieldName
            Case "PartNumber"
                e.KeyPressEventArgs.Handled = True
                If Not GetPartNumber(e.KeyPressEventArgs.KeyChar, EXTERNAL_NUMBER) Then
                    Exit Sub
                End If
                '--- Fill the Part Number cell with data
                Dim rci As New RowColumnIndex(r, c)
                gridOrders.View.GetPropertyAccessProvider().SetValue(gridOrders.GetRecordAtRowIndex(RowIndex), gridOrders.Columns(FieldName).MappingName, EXTERNAL_NUMBER)
                '--- THE ABOVE STATEMENT GENERATES A NULL REFERENCE EXCEPTION EVERY TIME
                gridOrders.MoveToCurrentCell(rci)
            Case Else
                Exit Sub
        End Select
    End Sub

    Private Function GetPartNumber(ByVal CharPressed As String, ByRef EXTERNAL_NUMBER As String) As Boolean
        ' This function opens a Parts Finder form and returns the selected part number
        Dim fh As New formPARTFINDER
        While True
            If CharPressed <> String.Empty Then
                fh.FirstChar = CharPressed
            End If
            fh.ShowDialog()
            If fh.ReturnStatus = -1 Then
                Exit While
            End If
            EXTERNAL_NUMBER = fh.ItemmNumber
            Exit While
        End While
        fh.Dispose()
        Return If(fh.ReturnStatus < 0, False, True)
    End Function

8 Replies

SA Saravanan Ayyanar Syncfusion Team January 23, 2020 01:53 PM UTC

Hi Tom, 
 
Thank you for using Syncfusion controls.   
 
We have checked your provided code example. In GetPartNumber() method you are creating the instance for formPARTFINDER.  We suspect that was Form, but you are using the properties like ItemNumber, FirstChar and ReturnStatus.  We are not cleared about this code snippet. Can you please mention that control name?  It will be helpful for us to investigate further. 
 
Regards, 
Saravanan A. 



TK Tom Krieg January 24, 2020 02:24 AM UTC

You are  correct, formPARTFINDER is a windows form shown as a dialog. The form has some textboxes and standard datagrids and the user enters text and makes choices to find a part number which the user then selects by clicking  a SELECT button. The form has public properties like ItemNumber and ReturnStatus. If I follow the code in the debugger, I can see that ReturnStatus and ItemNumber have the values I expect after the form closes and control returns to the calling method. The string value is then passed to the SetValue method.


TK Tom Krieg January 24, 2020 05:45 AM UTC

Update - the null exception error occurs with sfdatagrid cell of type checkbox (boolean) when I try and set the value "False". But with string values, the command executes successfully and, randomly (I can't work out a pattern) it doesn't display the new cell value (the cell remains blank) OR it displays the new cell value and then moves to a new line, creating a new row in the data grid. The balance of the row with the value in it is left blank.


SA Saravanan Ayyanar Syncfusion Team January 24, 2020 03:24 PM UTC

Hi Tom, 
 
Thank you for your update. 
 
We have checked your reported scenario. AddNewRow created the instance for data object while we enter the editing mode. In your code snippet, you try to assign the value for the new row, before creating the instance. So, it throws the null exception. You can resolve this by calling the sfDataGrid1.CurrentCell.BeginEdit() and sfDataGrid1.CurrentCell.EndEdit(). Please refer the below code snippet. 
 
AddHandler Me.sfDataGrid1.CurrentCellKeyPress, AddressOf SfDataGrid1_CurrentCellKeyPress 
 
Private Sub SfDataGrid1_CurrentCellKeyPress(ByVal sender As Object, ByVal e As CurrentCellKeyPressEventArgs) 
       Value = textBox1.Text 
       Value1 = Boolean.Parse(textBox2.Text) 
       Dim r As Integer = sfDataGrid1.CurrentCell.RowIndex 
       Dim c As Integer = sfDataGrid1.CurrentCell.ColumnIndex 
       Dim FieldName As String = sfDataGrid1.CurrentCell.Column.ToString() 
       Dim EXTERNAL_NUMBER As String = String.Empty 
 
       If Not sfDataGrid1.IsAddNewRowIndex(r) Then 
              Return 
       End If 
       If Not sfDataGrid1.View.IsAddingNew Then 
              Me.sfDataGrid1.CurrentCell.BeginEdit() 
              Me.sfDataGrid1.CurrentCell.EndEdit() 
       End If 
 
       Select Case FieldName 
              Case "CustomerName" 
                     e.KeyPressEventArgs.Handled = True 
 
                     '--- Fill the Part Number cell with data 
                     Dim rci As New RowColumnIndex(r, c) 
                     sfDataGrid1.View.GetPropertyAccessProvider().SetValue(sfDataGrid1.GetRecordAtRowIndex(rci.RowIndex), sfDataGrid1.Columns(FieldName).MappingName,Value) 
                     '--- THE ABOVE STATEMENT GENERATES A NULL REFERENCE EXCEPTION EVERY TIME                                                                                 
                     sfDataGrid1.MoveToCurrentCell(rci) 
       Case "Status" 
                     e.KeyPressEventArgs.Handled = True 
 
                     '--- Fill the Part Number cell with data 
                     Dim rci1 As New RowColumnIndex(r, c) 
                     sfDataGrid1.View.GetPropertyAccessProvider().SetValue(sfDataGrid1.GetRecordAtRowIndex(rci1.RowIndex), sfDataGrid1.Columns(FieldName).MappingName, Value1) 
                     '--- THE ABOVE STATEMENT GENERATES A NULL REFERENCE EXCEPTION EVERY TIME 
                     sfDataGrid1.MoveToCurrentCell(rci1) 
              Case Else 
                     Return 
       End Select 
End Sub 
   
Sample Link: 
 
Please let us know, if you require further assistance on this. 
 
Regards, 
Saravanan A. 



TK Tom Krieg January 25, 2020 04:02 AM UTC

I've got the code executing without any null exceptions. I now have another problem. The cell I'm attempting to set the value of remains blank. The code executes fine but nothing is shown onscreen. When I move to another cell, the original cell remains showing, blank,  in edit mode. In fact, if I delete the row, the shadow of the cell is still shown. I tried setting the value of 3 cells, one after the other. Cell 1 remains blank and cells 2 and 3 have the correct values inserted in them. If I then try and move the cursor to cell 4, the row highlight is removed. It seems that if the cell has focus, no value is inserted in that cell.

I'm starting to suspect that sfdatagrid isn't capable of doing what I want.


SA Saravanan Ayyanar Syncfusion Team January 27, 2020 02:25 PM UTC

Hi Tom, 
 
Thank you for your update. 
 
We have checked your reported scenario “data inserted cell(3) and move to next cell(4), the row highlight is removed” on our end, but the issue is not reproduced and it works as expected on our end. Please find the sample and video link below. 
 
Sample Link: 
 
Video Link: 
 
NOTE:  We have checked Syncfusion version 17.4.0.39. 
 
Can you please provide the following information? 
·         Please confirm your Syncfusion version. 
·         Can you please provided the replication procedure/video illustration of the issue. 
·         If possible, please revert us with the modified sample 
 
Please share the above details. It will be helpful for us to investigate further. 
 
Regards, 
Saravanan A. 
 



TK Tom Krieg January 28, 2020 06:07 AM UTC

Never mind. I have wasted so much time on trying to get sfdatagrid to work it's no longer worth it. I've been able to get the exact results I want with standard Microsoft DaraGridView in a couple of hours.


PR Padmini Ramamurthy Syncfusion Team January 29, 2020 09:02 AM UTC

Hi Tom Krieg, 

Glad that your issue resolved with Microsoft DataGridView. If you need any other assistance, please get back to us. We will happy to assist at any time. 

Regards, 
Padmini

Loader.
Live Chat Icon For mobile
Up arrow icon