This is a migrated thread and some comments may be shown as answers.

How to force empty grid cells to a value

3 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Herman Gouw
Top achievements
Rank 2
Herman Gouw asked on 21 Dec 2011, 08:10 AM
Hi,
I have to modifiy a working web application which I did back in 2009.

The application has some RadGrids which only accept numeric value on their cells.

Currently it is possible to leave the cells blank.

Can you please show me how to force empty grid cells to a certain value (in this case 0)?

The RadGrid cells are created as follows:

protected void rgOutages_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        GridNumericColumnEditor editor = (GridNumericColumnEditor)item.EditManager.GetColumnEditor("Unit_Outage_CountInflexible");
        editor.NumericTextBox.EmptyMessage = "0";
        editor.NumericTextBox.MaxLength = 8;
        editor.NumericTextBox.MaxValue = 999;
        editor.NumericTextBox.MinValue = 0;
        editor.NumericTextBox.NumberFormat.AllowRounding = true;
        editor.NumericTextBox.NumberFormat.DecimalDigits = 0;
        editor.NumericTextBox.Width = Unit.Percentage(100);
    }
}

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Dec 2011, 10:37 AM
Hello Herman,

One suggestion is you can access the cell and set the text as shown below.
C#"
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
  {
     TableCell cell = (e.Item as GridDataItem)["UniqueName"];
     cell.Text = "0";
  }
}

-Shinu.
0
Herman Gouw
Top achievements
Rank 2
answered on 06 Jan 2012, 04:30 AM
Hi,

Thanks for your reply but this is not really what I had in mind.
What needed is ... when the user blanks out any of the cell, I would like to force the empty cell to have a certain value (i.e. 0).

Regards,
Herman
0
Accepted
Veli
Telerik team
answered on 06 Jan 2012, 11:37 AM
Hi Herman,

In this case, you can use the OnValueChanging client event of the textboxes in your cells to reset the value to 0 if it is empty:

protected void rgOutages_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        GridNumericColumnEditor editor = (GridNumericColumnEditor)item.EditManager.GetColumnEditor("UnitPrice");
        //editor.NumericTextBox.EmptyMessage = "0";
        editor.NumericTextBox.MaxLength = 8;
        editor.NumericTextBox.MaxValue = 999;
        editor.NumericTextBox.MinValue = 0;
        editor.NumericTextBox.NumberFormat.AllowRounding = true;
        editor.NumericTextBox.NumberFormat.DecimalDigits = 0;
        editor.NumericTextBox.Width = Unit.Percentage(100);
        editor.NumericTextBox.ClientEvents.OnValueChanging = "numericValueChaning";
    }
}

<script type="text/javascript">
    function numericValueChaning(sender, args)
    {
        if (!args.get_newValue())
        {
            args.set_newValue("0");
        }
    }
</script>


Veli
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Herman Gouw
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Herman Gouw
Top achievements
Rank 2
Veli
Telerik team
Share this question
or