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

GridBoundColumn DataFormatString

3 Answers 362 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Somnath
Top achievements
Rank 1
Somnath asked on 25 May 2011, 08:51 AM
Hi
I have following code for a GridBoundColumn (Zip Code) 
<telerik:GridBoundColumn HeaderText="Zip Code" UniqueName="ZipCode" DataField="ZipCode"
     ItemStyle-Wrap="true" AllowSorting="false" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Left"
                   HeaderStyle-Width="9%"  ItemStyle-HorizontalAlign="Left" ItemStyle-Width="9%" DataFormatString="{0:#####-####}">                                                                                               
                           </telerik:GridBoundColumn>

but the DataFormatString property doesn't work properly........
let me know any solution..
Thanks in advance..

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 25 May 2011, 10:20 AM
Hello Somnath,

The following forum thread discusses the same scenario. Check the thread for more details.
DataFormatString Not working.

Thanks,
Princy.
0
Somnath
Top achievements
Rank 1
answered on 25 May 2011, 12:46 PM
Hi

can anyone provide javascript function as solution on the above issue?

Thanks,
0
Vasil
Telerik team
answered on 27 May 2011, 03:20 PM
Hello Somnath,

You could loop though the records on the client and change their text.
The code snippet bellow is an example how to format a template column with a label that contains nine digit number.

<script type="text/javascript">
 
  function formatZipCode(zipCode) {
 
    var template = "0:#####-####";
 
    var zIndex = 0, tIndex = 0;
    var result = "";
    for (tIndex = 0; tIndex < template.length; tIndex++) {
      if (template[tIndex] == '#') {
        result += zipCode[zIndex];
        zIndex++;
      }
      else {
        result += template[tIndex];
      }
    }
 
    return result;
  }
 
  function RowCreated(grid, args) {
    var zipCode = args.get_gridDataItem().get_element().children[0].children[0].textContent;
    zipCode = formatZipCode(zipCode);
    args.get_gridDataItem().get_element().children[0].children[0].textContent = zipCode;
  }
 
</script>
<script runat="server" type="text/C#">
  protected Random rand = new Random();
  protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
  {
    RadGrid1.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7 };
  }
  protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
  {
    GridDataItem item = e.Item as GridDataItem;
    if (item != null)
    {
      Label lbl = item["ZIP"].FindControl("Label1") as Label;
      int randomZip = 100000000 + rand.Next() % 900000000;
      lbl.Text = randomZip.ToString();
    }
  }
</script>
<div>
  <telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound"
    OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView>
      <Columns>
        <telerik:GridTemplateColumn UniqueName="ZIP">
          <ItemTemplate>
            <asp:Label runat="server" ID="Label1"></asp:Label>
          </ItemTemplate>
        </telerik:GridTemplateColumn>
      </Columns>
    </MasterTableView>
    <ClientSettings>
      <ClientEvents OnRowCreated="RowCreated" />
    </ClientSettings>
  </telerik:RadGrid>

Keep in mind that you will need to make additional verification of the data before trying to format it client side.

Greetings,
Vasil
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Somnath
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Somnath
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or