GridView - How to clear numeric field

1 Answer 158 Views
GridView
Brian
Top achievements
Rank 1
Brian asked on 03 Dec 2021, 08:06 AM

Hi,

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

Sort by
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:

private void RadGridView_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:Static x:Key="emptyString" Member="sys:String.Empty"/>
</Window.Resources>

<Grid>
    <telerik:RadGridView ItemsSource="{Binding DataTable.DefaultView}" 
                                       AutoGenerateColumns="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding ProductName}" Header="Product name"/>
            <telerik:GridViewDataColumn DataMemberBinding="{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/.

Brian
Top achievements
Rank 1
commented on 07 Dec 2021, 10:20 PM

Thanks,

It worked ...

/Brian

 

 

Tags
GridView
Asked by
Brian
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or