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

RadGrid Field Formatting

7 Answers 163 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Computer Surgeon
Top achievements
Rank 1
Computer Surgeon asked on 17 Dec 2008, 11:46 AM
Hi,
     I use DataFormatString  for mobile number. It works fine. The issue i face is... When the value binding to this field is empty or null then the formatting looks like ()-

I need Mobile no to look like   (123)456-7890
If the binding number is empty then it looks like ()-
I need the formatting should not applied if binding mobile is empty or null. So the data displayed will be blank.

<telerik:GridBoundColumn DataType="System.Int64" HeaderButtonType="TextButton" DataField="MobileNo" DataFormatString="{0:(###)###-####}" HeaderText="Mobile"
                                                                            UniqueName="MobileNo">
                                                                            <ItemStyle  HorizontalAlign="Left" />
                                                                            <HeaderStyle HorizontalAlign="Center" />
                                                                        </telerik:GridBoundColumn>

Please help to get ride of this issue

Thanks
Happy coding...  ;-)

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 17 Dec 2008, 11:56 AM
Hello,

It's possible to use the ItemDataBound event and check whether the value is null or empty as shown below:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    GridDataItem item = e.Item as GridDataItem; 
    if (item != null && string.IsNullOrEmpty(item["MobileNo"].Text)) 
        item["MobileNo"].Text = ""

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Computer Surgeon
Top achievements
Rank 1
answered on 17 Dec 2008, 12:39 PM
Hi,

          Thanks for your immediate response. I thought there might be a property unknown to me or some other solution instead of going to RadGrid's ItemDataBound. If you have any other idea other than going to ItemDataBound that will be  great.

Thanks!
Happy Coding ... ;-)
0
Computer Surgeon
Top achievements
Rank 1
answered on 17 Dec 2008, 12:55 PM
Hi,

         I use Zip Code Formatting . DataFormatString is #####-####
         First 5 numbers are mandatory that i validate using javascript. Next following 4 numbers are optional.
        1.)  If user provide 12345-6789 valid zip its okay
        2.)  If user provide 12345-____ valid zip its okay

           My issue arises in 2nd case. While displaying the value entered 12345-____ in RadGrid it displayed like 1-2345


<telerik:GridBoundColumn DataType="System.Int64" DataField="Zip" DataFormatString="{0:#####-####}" HeaderButtonType="TextButton" HeaderText="Zip Code"
                                                                            SortExpression="Zip" UniqueName="Zip">
                                                                            <ItemStyle HorizontalAlign="left" />
                                                                            <HeaderStyle HorizontalAlign="Center" />
                                                                        </telerik:GridBoundColumn>

Please suggest how to get ride of this...

Thanks!
Happy Coding... ;-)
0
Steven
Top achievements
Rank 2
answered on 17 Dec 2008, 02:14 PM
Hi,

I have a similar problem.
I don't have a datasourcecontrol. I build a custom generic list in my code behind and then databind this list to the grid, the columns are generated on the fly. one of my fields in the list is CreatedOn, which holds the date I want to format. However I am not sure how to achieve this.

I tried the following which gives me an error:

Protected Sub rGPreOrderHistory_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rGPreOrderHistory.ItemDataBound 
 
            Dim item As GridDataItem = TryCast(e.Item, GridDataItem) 
            If item("CreatedOn") IsNot Nothing AndAlso item("CreatedOn").Text IsNot String.Empty AndAlso item("CreatedOn").Text <> "" Then 
                item("CreatedOn").Text = Convert.ToDateTime(item("CreatedOn").Text).ToString("dd/MM/yyyy"
            End If 
 
        End Sub 

it gives a System.NullReferenceException

What am I missing?

0
Steven
Top achievements
Rank 2
answered on 18 Dec 2008, 11:45 AM
I have edited the code to the following and now it works... but ONLY for the 1st row. seems like the event is only firing once?
How do I fix this to apply the formatting to every row in the grid?

Protected Sub rGPreOrderHistory_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rGPreOrderHistory.ItemDataBound 
            If e.Item.ItemType = GridItemType.Item Then 
                Dim item As GridDataItem = TryCast(e.Item, GridDataItem) 
                item("CreatedOn").Text = Convert.ToDateTime(item("CreatedOn").Text).ToString("dd/MM/yyyy"
            End If 
        End Sub 

0
Steven
Top achievements
Rank 2
answered on 18 Dec 2008, 02:31 PM
prob solved with following code.

Protected Sub rGPreOrderHistory_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rGPreOrderHistory.ItemDataBound 
            If TypeOf e.Item Is GridDataItem Then 
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem) 
                item("CreatedOn").Text = Convert.ToDateTime(item("CreatedOn").Text).ToString("dd/MM/yyyy"
            End If 
        End Sub 

0
Daniel
Telerik team
answered on 20 Dec 2008, 09:35 AM
Hello guys,

@Steven: We're glad that you managed to resolve the problem. Let us know if you have more questions.

About the ZIP code. I created a simple example using TemplateColumn and RadMaskedTextBox control. Please find it attached to this post.

Regards
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Computer Surgeon
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Computer Surgeon
Top achievements
Rank 1
Steven
Top achievements
Rank 2
Share this question
or