I have a GridView bound to a DataTable. The DataTable contains a combinition of numeric and non numeric columns. Null is a valid value in the the numeric columns. How do I let the user clear an existing numeric value to null?
/Brian
1 Answer, 1 is accepted
0
Stenly
Telerik team
answered on 07 Dec 2021, 03:58 PM
Hello Bryan,
If the columns of the RadGridView control are auto-generated, you could subscribe to the AutoGeneratingColumn event. In it, you could get the "units in stock"(from the provided image) column via the event arguments, cast it to GridViewDataColumn type, and set the TargetNullValue property to an empty string. The following code sample shows this suggestion's implementation:
privatevoidRadGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
if (e.Column.Header.ToString() == "units in stock")
{
((GridViewDataColumn)e.Column).DataMemberBinding.TargetNullValue = string.Empty;
}
}
Please note that the above-mentioned event will only occur if the AutoGenerateColumns property of the grid view is set to True.
Alternatively, if the columns are defined manually, you could follow the same approach with setting the TargetNullValue property to an empty string.
<Window.Resources><x:Staticx:Key="emptyString"Member="sys:String.Empty"/></Window.Resources><Grid><telerik:RadGridViewItemsSource="{Binding DataTable.DefaultView}"AutoGenerateColumns="False"><telerik:RadGridView.Columns><telerik:GridViewDataColumnDataMemberBinding="{Binding ProductName}"Header="Product name"/><telerik:GridViewDataColumnDataMemberBinding="{Binding Quantity, TargetNullValue={StaticResource emptyString}}"Header="units in stock"/></telerik:RadGridView.Columns></telerik:RadGridView></Grid>
With that said, I hope the provided information is of help to you. If this does not work for you, please send over a small sample project which demonstrates your current setup and I will gladly try to provide a solution for your particular scenario.
Regards,
Stenly
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.