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

Different cell format based on the content on another cell on the same row

Hi, I kow it's a strange request, but I need to know if in a datagrid is possible to change the format of a cell from c2 to P based on the content of another cell on the same row.

I tried something using QueryCellStyle Event:


    Private Sub dgCostiRicavi_QueryCellStyle(ByVal sender As Object, ByVal e As QueryCellStyleEventArgs)

If e.DataRow.RowData.Label = "Irap" And e.Column.MappingName = "ricavo" Then
e.Column.Format="p"
Else
If e.Column.MappingName = "ricavo" Then
e.Column.Format = "c2"
End If
End If
End Sub

but it change the format for the row after the one I need.



2 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team March 15, 2023 01:49 PM UTC

Hi Michele,


We have checked the feasibility of achieving your requirement. Whenever you change the format of a column, it will be applied to all the rows in that column. However, you can achieve your requirement to "Different cell format based on the content in another cell on the same row" by using the FormatProvider shown below,


sfDataGrid1.Columns("Quantity").Format = "C2"

 

' Change the format by using the CustomFormatter.

sfDataGrid1.Columns("Quantity").FormatProvider = New CustomFormatter()

Public Class CustomFormatter

       Implements IDataGridFormatProvider

       Public Function Format(ByVal format_Renamed As String, ByVal gridColumn As GridColumnBase, ByVal record As Object, ByVal value As Object) As Object Implements IDataGridFormatProvider.Format

             If value Is Nothing Then

                    Throw New ArgumentNullException(If(value Is Nothing, "format", "args"))

             End If

             Dim orderInfo = TryCast(record, OrderInfo)

 

             ' Here you can change the format for the Qunatity column as P if the OrderID has the value 10005.

             If TypeOf gridColumn Is GridColumn AndAlso (TryCast(gridColumn, GridColumn)).MappingName = "Quantity" AndAlso orderInfo.OrderID = 10005 Then

                    Return (Convert.ToInt32((orderInfo.Quantity.ToString()))).ToString("P")

             End If

             Return (Convert.ToInt32((orderInfo.Quantity.ToString()))).ToString("C2")

       End Function

       Public Function GetFormat(ByVal formatType As Type) As Object Implements System.IFormatProvider.GetFormat

             Return Me

       End Function

       Public Function Format(ByVal format_Renamed As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String Implements System.ICustomFormatter.Format

             Throw New NotImplementedException()

       End Function

End Class


Here, we have prepared the sample with the above suggestion, please have a look at this.


For more information related to the customization of the column format, please refer to the below knowledge base documentation link,


KB Link: https://www.syncfusion.com/kb/12130/how-to-customize-the-column-format-in-winforms-datagrid-sfdatagrid


Regards,

Dhanasekar M.


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


Attachment: VB_92311b61.zip

Marked as answer

MI Michele March 15, 2023 07:38 PM UTC

Thanks it works perfectly.

Regards

Michele


Loader.
Up arrow icon