Save/Display database BigInt timespan.ticks as text

4 posts, 0 answers
  1. Rick Hubka
    Rick Hubka avatar
    33 posts
    Member since:
    Apr 2006

    Posted 15 Apr 2012 Link to this post

    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.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Apr 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rick Hubka
    Rick Hubka avatar
    33 posts
    Member since:
    Apr 2006

    Posted 17 Apr 2012 Link to this post

    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
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Apr 2012 Link to this post

    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.
Back to Top