How to change whole column value programatically with a button click?

Please provide solution in Vb.net

I want to change all row in column name Discount1 to say 75.00 on a button click. How do I achieve this?


1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team June 13, 2023 02:50 PM UTC

Hi Urvik,

Your requirement to change whole column value programmatically with a button click in SfDataGrid can be achieved by using View.GetPropertyAccessProvider.SetValue method. Refer to the below code snippet,

Private Sub OnChangeCellValueClicked(ByVal sender As Object, ByVal e As System.EventArgs)

     ' Check the view is not null and the table control is not null

     If sfDataGrid1.View IsNot Nothing AndAlso sfDataGrid1.TableControl IsNot Nothing AndAlso sfDataGrid1.View.Records IsNot Nothing Then

         ' Loop through the all records in the view based on Grouping applied or not and set the value for particular column in the record

         For i As Integer = 1 To (If(sfDataGrid1.View.TopLevelGroup IsNot Nothing, sfDataGrid1.View.TopLevelGroup.DisplayElements.Count, sfDataGrid1.View.Records.Count))

             ' Get the row index based on the record index

             Dim recordIndex = sfDataGrid1.TableControl.ResolveToRecordIndex(i)

 

             If recordIndex < 0 Then

                 Continue For

             End If

 

             Dim record As Object = Nothing

 

             ' Get the record when grouping applied in SfDataGrid

             If sfDataGrid1.View.TopLevelGroup IsNot Nothing Then

                 Dim displayElement = sfDataGrid1.View.TopLevelGroup.DisplayElements(recordIndex)

 

                 If displayElement Is Nothing Then

                     Continue For

                 End If

                 If TypeOf displayElement Is RecordEntry Then

                     record = CType(displayElement, RecordEntry).Data

                 End If

             Else ' Get the record when grouping is not applied in SfDataGrid

                 record = sfDataGrid1.View.Records(recordIndex).Data

             End If

 

             'Change the cell value for particular column (Discount1) in the record

             If record IsNot Nothing Then

                 sfDataGrid1.View.GetPropertyAccessProvider().SetValue(record, "Discount1", "75")

             End If

         Next i

     End If

End Sub

UG Link: https://help.syncfusion.com/windowsforms/datagrid/editing?cs-save-lang=1&cs-lang=vb#change-a-cell-value-programmatically

Find the sample demo in the attachment.

Regards,
Vijayarasan S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Attachment: SfDataGridDemo_2436618.zip

Marked as answer
Loader.
Up arrow icon