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

Custom formatting in RadGrid

2 Answers 101 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adilson Carvalho
Top achievements
Rank 1
Adilson Carvalho asked on 27 Sep 2010, 03:46 PM
Hi folks,

I recieve a time duration data in seconds, lets say 90, and I need to show it as 1:30

I can just use TimeSpan.FromSeconds(90).ToString() to get this done for me, but I don't know where I can put it on RadGrid.

I only see a FormatString property, and I need to provide a little logic. Any suggestions?

Thanks,

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 Sep 2010, 06:34 AM
Hello Adilson,

You can access the cell value in ItemDataBound event and can convert it into appropriate format. Sample code is given below.

ASPX:
<telerik:GridBoundColumn DataField="Duration" HeaderText="Duration" UniqueName="Duration">
</telerik:GridBoundColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            Int32 duration = Convert.ToInt32(item["Duration"].Text); // access cell value using ColumnUniqueName
            item["Duration"].Text = TimeSpan.FromMinutes(duration).ToString(); //converting
        }
    }

-Shinu.
0
Adilson Carvalho
Top achievements
Rank 1
answered on 28 Sep 2010, 01:54 PM
Thanks Shinu,

Before I got your reply I tried the code below, wich also worked, but I think your solution is smaller in code size and easier to maintain.

I will post mine here, maybe this is usefull to someone.

  • First I used GridTemplateColumn

<telerik:GridTemplateColumn DataField="TempoLogado" HeaderText="Tempo Total" HeaderTooltip="Tempo total de serviço do operador"
    UniqueName="TempoLogado">
    <ItemTemplate>
        <uc1:LabelHoras ID="MaisAntigaLabel" runat="server" TempoEmSegundos='<%# Bind("DuracaoEstado") %>' />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</telerik:GridTemplateColumn>


  • Then I've create a specific label to show the data, wich formats the data my way

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

private void SetTotalSegundos(int totalSeconds)
{
    Label1.Text = TimeSpan.FromSeconds(totalSeconds).ToString();
}


As I said, it works, but it isn't wrist friendly :)

 

 

 

Tags
Grid
Asked by
Adilson Carvalho
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Adilson Carvalho
Top achievements
Rank 1
Share this question
or