get and set values in the grid

To change or find the value of a cell using the GridDataboundGrid I used the following line of code

gridDataBoundGrid({RowNumber}, {ColumnNumber}).CellValue = 50

How do I set a cell value or read a cell value using the SfDataGrid using vb.net programming code?







8 Replies

DG Derek Geldart July 24, 2018 06:45 PM UTC

I noticed that there is another post with code that will read the value but it takes about 40 lines of code.  Is there a way to reduce the lines of code to closer to one?  And how does one set a value of a particular cell?


FP Farjana Parveen Ayubb Syncfusion Team July 25, 2018 12:21 PM UTC

Hi Derek,   
   
Thanks for contacting Syncfusion support.   
   
GridDataBound is a cell-based grid. So, you can get the cell value directly by using row and column index. But SfDataGrid is a data bound control. To get the cell value of particular row and column index, you can use the SfDataGrid.View.Records in SfDataGrid. Please refer the below code example,   
   
Code Example   
  
Private Sub button1_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles button1.Click   
       ' Get the cell value for RowIndex = 5 and ColumnIndex = 3   
       Dim rowIndex As Integer = 5   
       Dim columnIndex As Integer = sfDataGrid.TableControl.ResolveToGridVisibleColumnIndex(3)   
       If columnIndex < 0 Then   
              Return   
       End If   
       Dim mappingName = sfDataGrid.Columns(columnIndex).MappingName   
       Dim recordIndex = sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex)   
       If recordIndex < 0 Then   
              Return   
       End If   
       If sfDataGrid.View.TopLevelGroup IsNot Nothing Then   
              Dim record = sfDataGrid.View.TopLevelGroup.DisplayElements(recordIndex)   
              If Not record.IsRecords Then   
                     Return   
              End If   
              Dim data = (TryCast(record, RecordEntry)).Data   
              Dim cellVaue = (data.GetType().GetProperty(mappingName).GetValue(data, Nothing).ToString())   
       Else   
              Dim record1 = sfDataGrid.View.Records.GetItemAt(recordIndex)   
              Dim cellVaue = (record1.GetType().GetProperty(mappingName).GetValue(record1, Nothing).ToString())   
       End If   
End Sub   
   
   
You can set the cell value for a row and column index by getting the ItemPropertiesProvider in SfDataGrid.View. Please refer the below code example   
   
Code Example   
 
Private Sub button2_Click(ByVal sender As ObjectByVal e As System.EventArgsHandlesbutton2.Click   
       ' Set the cell value for RowIndex = 2 and ColumnIndex = 1   
      Me.sfDataGrid.View.GetPropertyAccessProvider().SetValue(sfDataGrid.GetRecordAtRowIndex(2), sfDataGrid.Columns(1).MappingName, "Syncfusion")   
End Sub   
   
  
Regards,   
Farjana Parveen A   



DG Derek Geldart July 30, 2018 07:17 PM UTC

Thank you the sample code works for any data in which the record already exists.  When I try to set the value of a cell on a row that is being added new I get an error.  Is there a way to set cell values of a row that is being added new?

I tried using the following code to tell the program to look at the new row line

If (gridPipe.CurrentCell.RowIndex) = gridPipe.GetAddNewRowIndex Then
                gridPipe.TableControl.Invalidate(gridPipe.TableControl.GetRowRectangle(gridPipe.GetAddNewRowIndex(), False))
                Dim record2 = gridPipe.View.Records.GetItemAt((gridPipe.CurrentCell.RowIndex))
                Me.gridPipe.View.GetPropertyAccessProvider().SetValue(record2, gridPipe.Columns("SalesType").MappingName, "CDN SALES")
End If

When I run the code I get an error on the Dim record2 saying the index is out of range - in others words the add new line is still not being acknowledged.  


 


MA Mohanram Anbukkarasu Syncfusion Team August 1, 2018 12:17 PM UTC

Hi Derek, 
 
Thanks for your update. 
 
You can set value using ItemPropertiesProvider for any record which is already added to the underling DataSource. This cannot be used to set value for the AddNewRow.  
 
Instead you can set any default value for cells in the AddNewRow while initiating it by using the SfDataGrid.AddNewRowInitiating event. Please refer to the below code example and sample from the given location. 
 
Code Example : 
 
AddHandler Me.sfDataGrid.AddNewRowInitiating, AddressOf sfDataGrid_AddNewRowInitiating 
 
Private Sub sfDataGrid_AddNewRowInitiating(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs) 
       Dim data = TryCast(e.NewObject, OrderInfo) 
       data.OrderID = 101 
End Sub 
 
 
 
Regards, 
Mohanram A. 



DG Derek Geldart August 1, 2018 04:57 PM UTC

The "OrderInfo" in the above code is a reference to a class.  I tried the following code to set my type to the dataset I am using as a datasource to my grid

Private Sub gridpipe_AddNewRowInitiating(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs)
        Dim data1 = TryCast(e.NewObject, Sales20._20SalesRow)
        data1.Customer = "MyCustomer"
    End Sub

but I got the attached error 

When using a dataset and dataadapter how does one Dim the data1?




MA Mohanram Anbukkarasu Syncfusion Team August 2, 2018 12:41 PM UTC

Hi Derek, 
 
Thanks for your update. 
 
While using DataSet as DataSource, the value for the AddNewRow cells can be set by casting the NewObject property to DataRowView. Please refer to the below code example. 
 
Code Example: 
AddHandler Me.sfDataGrid.AddNewRowInitiating, AddressOf sfDataGrid_AddNewRowInitiating 
 
Private Sub sfDataGrid_AddNewRowInitiating(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs) 
       Dim data = TryCast(e.NewObject, System.Data.DataRowView) 
       data.Row("ID") = 1010 
       data.Row("Name") = "James" 
End Sub 
 
Regards, 
Mohanram A. 



DG Derek Geldart August 3, 2018 01:52 PM UTC

Thank you so very much, this works great!


MA Mohanram Anbukkarasu Syncfusion Team August 6, 2018 11:41 AM UTC

Hi Derek,  
 
Thanks for your update. 
 
We are glad to know that the given solution has solved your query. Please let us know, If you need any further assistance. 
 
Regards, 
Mohanram A. 


Loader.
Up arrow icon