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.
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() 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,
Regards,
Dhanasekar M.
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Thanks it works perfectly.
Regards
Michele