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

RadGrid Column Formatting Question

2 Answers 317 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 13 Jan 2011, 10:38 AM
I have a RadGrid that is bound at runtime to a Stored Procedure that returns a dynamic set of columns, the SP is a pivot of money and dates.  So the SP always returns Column 1 and Column 2 that are always the same name but then the other columns are dynamic depending on what dates and costs have been entered by the users i.e. the SP could return Column 1 and Column 2 plus 1 additional Column (which will be named the month and Year selected e.g. Jul 2011 or it could return 10 additional columns Spanning Jul 2011 to April 2012 and so on.  These addtitional columns return a decimal value, my question is how can i format these additional columns (that will change each time data is added) as DataFormatString="{0:£###,##}" if i dont have a grid bound column.

My Grid is simply
<telerik:RadGrid ID="GridMonies" runat="server">
</telerik:RadGrid>

Which is bound at runtime using
GridMonies.DataSource = WSA.PaymentDrawdownView(WSARecord)
GridMonies.DataBind()

Thanks in advance

Tony

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Jan 2011, 11:14 AM
Hello Anthony,

Since you are automatically generating columns in grid, you can use ColumnCreated event to set DataFormatString of auto generated GridBoundColumn.
C#:
protected void RadGrid2_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
   {
       if (e.Column is GridBoundColumn)
       {
           if (e.Column.UniqueName == "column_name")//check with name of column
           {
               (e.Column as GridBoundColumn).DataFormatString = "{0:£###,##}";
          }
       }
   }

Or you can do the same in ItemDataBound event like below.

protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
   {
      if(e.Item is GridDataItem)
      {
          GridDataItem item = (GridDataItem)e.Item;
          TableCell cell = item["column_name"];
          Int32 value =Convert.ToInt32(cell.Text);
          cell.Text = String.Format("{0:£###,##}", value);
       }
  }

Thanks,
Princy.
0
Anthony
Top achievements
Rank 1
answered on 13 Jan 2011, 11:53 AM
Thanks Princy

Had an issue with displaying the format as "{0:&pound;###,##}" when i had this value the columns simply displayed the £ sign and not the values, got around this by using "{0:C0}" instead.

Protected Sub GridMonies_ColumnCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles GridMonies.ColumnCreated
    If TypeOf e.Column Is GridBoundColumn Then
        If e.Column.UniqueName <> "DeliverableOrExpense" Or e.Column.UniqueName <> "DeliverableDesc" Then
            TryCast(e.Column, GridBoundColumn).DataFormatString = "{0:C0}"
        End If
    End If
End Sub

Thanks again
Tags
Grid
Asked by
Anthony
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Anthony
Top achievements
Rank 1
Share this question
or