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

Save/Display database BigInt timespan.ticks as text

3 Answers 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rick Hubka
Top achievements
Rank 1
Rick Hubka asked on 15 Apr 2012, 07:37 PM
In a RadGrid, I have a requirement to display running sports time as a text mask of hour:min:sec (00:00:00) but store this in the database as a BigInt of timepan.ticks.
I have the 2 methods to convert from one to another show below.
The application is being converted from Devexpress ASPxGridView to Telerik RadGrid.  I show the existing devexpress grid methods used below.
// Called from Devexpress ASPxGridView1_ParseValue on Insert/Update
private static long LongFromString(Object value)
{
    if (value == null || String.IsNullOrEmpty((String)value))
    {
        return 0;
    }
    else
    {
        TimeSpan span =  TimeSpan.Parse((String)value);
        return span.Ticks;
    }
}
 
Here, one method is for the Grid rows and one is for edit mode.
// Called from Devexpress ASPxGridView1_CellEditorInitialize on Select
// AND Devexpress ASPxGridView1_CustomColumnDisplayText  on Select
private static String StringFromBigInt(Object Value)
{
    TimeSpan span = new TimeSpan((long)Value);
    if (span.Days != 0)
        return span.ToString("c");
 
    String str = span.ToString(@"hh\:mm\:ss");
    return String.Format("{0}", str);
}
Can someone tell me what RadGrid methods will allow me to have the same functionality to Display 00:00:00 but save ti the database as a BigInt.
Thanks so much for your help.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Apr 2012, 03:23 PM
Hi Rick Hubka,

I suppose that you just want to display the BigInt value from the database as "00:00:00" format. Please take a look into the following sample code.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
  {
    if (e.Item is GridDataItem)
     {
      GridDataItem ditem = (GridDataItem)e.Item;
      TableCell Time = (TableCell)ditem["Time"];
      string text=(Time.Text);
      HH = str.Substring(0,2)+ ":";
      MM = str.Substring(2, 2)+ ":";
      SS = str.Substring(4, 2) ;
      string time = HH + MM + SS;
      Time.Text = time;
     }
  }

Thanks,
Shinu.
0
Rick Hubka
Top achievements
Rank 1
answered on 17 Apr 2012, 06:12 AM
Nice.  Thanks Shinu
I'm half way there.  Using your code above, This code below now converts the BigInt from the database and populate the RadGrid text. Works
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 {
     if (e.Item is GridDataItem)
     {
         GridEditableItem edititem = (GridEditableItem)e.Item;
         TableCell Duration = (TableCell)edititem["Duration"];
         Duration.Text = StringFromBigInt(Convert.ToInt64(Duration.Text));
     }
 }
Now... how can I do this the other way and intercept the insert/update of the edit text?
I'll use my long = LongFromString(string) method to convert that string data to a long and need to save that long to the database as a BigInt.  What RadGrid event does this pre-save intercept and will allow me to convert before the save?

Please and Thanks
0
Shinu
Top achievements
Rank 2
answered on 17 Apr 2012, 08:06 AM
Hi Rick Hubka,

If you want to convert the time user entered through insert/update form from string to Bigint , you can do it before inserting/updating to database. For that please try your function 'StringFromBigInt' in Insert/UpdateCommand like below.

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
   GridEditableItem editItem = (GridEditableItem)e.Item;
   //your code
}
 
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
   GridDataInsertItem Iitem = (GridDataInsertItem)e.Item;
   //your code
}

Thanks,
Shinu.
Tags
Grid
Asked by
Rick Hubka
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Rick Hubka
Top achievements
Rank 1
Share this question
or