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

Exception on "string formatting in edit mode when new row is added"

2 Answers 50 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 19 Jan 2014, 04:52 PM
I need to format few strings when in edit mode. In line I have done it with the designer.
This is the code I am using for the formatting in edit mode:

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
         
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem edit = (GridEditableItem)e.Item;
                TextBox txt = (TextBox)edit["Due"].Controls[0];
                string dataFormatString = (RadGrid1.MasterTableView.GetColumn("Due") as GridBoundColumn).DataFormatString;
                txt.Text = String.Format(dataFormatString, double.Parse(txt.Text));
            }
 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem edit = (GridEditableItem)e.Item;
                TextBox txt = (TextBox)edit["Paid"].Controls[0];
                string dataFormatString = (RadGrid1.MasterTableView.GetColumn("Paid") as GridBoundColumn).DataFormatString;
                txt.Text = String.Format(dataFormatString, double.Parse(txt.Text));
            }
 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem edit = (GridEditableItem)e.Item;
                TextBox txt = (TextBox)edit["Expenses"].Controls[0];
                string dataFormatString = (RadGrid1.MasterTableView.GetColumn("Expenses") as GridBoundColumn).DataFormatString;
                txt.Text = String.Format(dataFormatString, double.Parse(txt.Text));
            }
 
 
    }
The problems I have are the following:

1) When I click add a new row I get an exception because the text boxes are still empty, so the above methods try to format a null value. I do I get around this problem ?
2) The above code formats the numeric string, how do I format the other fields containing date time? I need {0:MMM/yyyy}

Thanks for supporting,
Felice

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 20 Jan 2014, 03:59 AM
Hi Felice,

In order to avoid the exception being raised during insert you can avoid the format during insert try the following if condition and all the columns can be edited using a single loop instead of repeating it.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        //For a bound column with Numeric data
        TextBox txtnum = (TextBox)edit["ShipVia"].Controls[0];
        double dblnum = Double.Parse(txtnum.Text);
        string strnum = String.Format("{0:C}", dblnum);
        txtnum.Text = strnum;
        //For a bound column with Datetime data
        TextBox textBox = (TextBox)edit["OrderDate"].Controls[0];
        textBox.Text = Convert.ToDateTime(textBox.Text).ToString("MMM/yyyy");       
    }
        
}

Another possible way to set Format string for a DateTime column is that you can use a GridDateTimeColumn and set its EditDataFormatString property to the format you need. This format will be applied to the date picker that will be initialized for editing values under this column.

ASPX:
<telerik:GridDateTimeColumn DataField="OrderDate" HeaderText="OrderDate" UniqueName="OrderDate"
 DataFormatString="{0:MMM/yyyy}" EditDataFormatString="MMM/yyyy">
</telerik:GridDateTimeColumn>

Thanks,
Princy
0
Felice
Top achievements
Rank 1
answered on 20 Jan 2014, 08:02 AM
Dear Princy,
thank you. Your snippet solved my problem. It works perfectly.
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Felice
Top achievements
Rank 1
Share this question
or