skip to Main Content

I have a column generated as int from datasource, how can I change it to dispay minutes and seconds? Is there a property in Gridview?
For example. if I have value 70 to be shows as 01:10 or 1m 10s.
Edit: I did an event as suggested in comments :

   protected void Grid_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e)
            {
                try
                {
                    if (e.Column.FieldName == "AdsDisplayedSeconds" || e.Column.FieldName == "UndockedSeconds")
                    {
                        TimeSpan t = TimeSpan.FromSeconds(Convert.ToInt32(e.Value.ToString()));
                        e.Value = string.Format("{0:D2}:{1:D2}",
                                            t.Minutes,
                                            t.Seconds);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.LogError(ex);
                }
            }

When I am debugging, for record 70 for example, e.Value has value "01:10", but them in grid it is shown 70

3

Answers


  1. I guess you mean TimeSpan instead DateTime.

    Assumming your int always represents seconds:

    TimeSpan ts = TimeSpan.FromSeconds(70);
    Console.WriteLine(ts.ToString());
    
    Login or Signup to reply.
  2. You can use converter to achive that

    [ValueConversion(typeof(int), typeof(string))]
    public class DateToNeededFormat : IValueConverter
    {
        public object Convert(object? value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            TimeSpan time = TimeSpan.FromSeconds((int)value);
                
            return time.ToString(@"hh:mm:ss:fff", DateTimeFormatInfo.InvariantInfo);
        }
     
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var strValue = value.ToString();
            
            return TimeSpan.TryParse(strValue, out TimeSpan result )? result.TotalSeconds : value;
        }
    }
    

    Than you need add resourse to Application.Resources

       <local:DateToNeededFormat x:Key="DateConverter"/>
    

    After all you can use it next way

     <GridViewColumn 
            Header="Time" 
            DisplayMemberBinding="{Binding Path=Time, Converter={StaticResource dateConverter}}"
        />
    
    Login or Signup to reply.
  3. Assuming a column in the GV with the int value?

    Say, like this:

            <asp:GridView ID="GHotels" runat="server" CssClass="table" AutoGenerateColumns="false"
                width="45%" DataKeyNames="ID" OnRowDataBound="GHotels_RowDataBound" AllowPaging="True"
                OnPageIndexChanging="GHotels_PageIndexChanging"   >
                <Columns>
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                    <asp:BoundField DataField="LastName" HeaderText="LastName"    />
                    <asp:TemplateField HeaderText="HotelName">
                        <ItemTemplate>
                            <asp:Label ID="txtHotel" runat="server" Text='<%# Eval("HotelName") %>' ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="270" />
    
                    <asp:TemplateField HeaderText="Seconds">
                        <ItemTemplate>
                            <asp:Label ID="txtSeconds" runat="server" Text='<%# Eval("Seconds") %>' ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                
                
                </Columns>
    
    
                <PagerStyle CssClass="GridPager" />
            </asp:GridView>
            
    

    code to load, say this:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }
    
        void LoadData()
        {
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                string strSQL = 
                    "SELECT * FROM tblHotelsA ORDER BY HotelName";
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                    DataTable rstData = new DataTable();
                    conn.Open();
                    rstData.Load(cmdSQL.ExecuteReader());
                    GHotels.DataSource = rstData;
                    GHotels.DataBind();
                }
            }
        }
    

    And we now have this:

    enter image description here

    So, we have that column with seconds – we want to convert to HH:mm:ss

    We can use the row data bound event, and add this code:

        protected void GHotels_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label txtSeconds = e.Row.FindControl("txtSeconds") as Label;
                TimeSpan MySeconds = new TimeSpan(0,0,Convert.ToInt32(txtSeconds.Text));
    
                txtSeconds.Text = MySeconds.ToString(@"hh:mm:ss");
            }
        }
    

    And now we get:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search