Number formatting

5 posts, 1 answers
  1. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 13 Mar 2012 Link to this post

    Is it possible to format a column using number formats or date formats?

    Thanks,

    Javier Gonzalez de Aragon
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 14 Mar 2012 Link to this post

    Hi Javier,

    Have a look at the help topic on data formatting which should give you the answers.
    If you need further help though, then just let me know
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 14 Mar 2012 Link to this post

    That help topic refers to GridView and I need to format columns in ListView. Does it apply as well?

    Thanks,

    Javier Gonzalez de Aragon
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 15 Mar 2012 Link to this post

    Hi Javier,

    My apologies for giving you a link to the incorrect topic.
    You can't pass a list view column a format, but you could subscribe (for exmaple) to the VisualItemFOrmatting event to format the text
    this.radListView1.VisualItemFormatting += new ListViewVisualItemEventHandler(radListView1_VisualItemFormatting);

    void radListView1_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
    {
     
    }

    There may be other ways too depending on your requirements. Let me know if you need more information though
    Richard

  6. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 16 Mar 2012 Link to this post

    Hi Javier,

    Richard's suggestion will work in SimpleView and IconView since the items in these two views are simple. However, the visual items in DetailView are complex and contain a number of cells, each with a different value. Therefore, to format the data in cells that belong to a given column, you should use the CellFormatting event. The following code snippet demonstrates how you can format the values of type DateTime in the second column:
    public partial class Form1 : RadForm
    {
        public Form1()
        {
            InitializeComponent();
             
            this.radListView1.Columns.Add("Column1");
            this.radListView1.Columns.Add("Column2");
            this.radListView1.ViewType = ListViewType.DetailsView;
     
            for (int i = 0; i <= 100; i++)
            {
                this.radListView1.Items.Add("Item " + i, DateTime.Now.AddHours(i));
            }
     
            this.radListView1.CellFormatting += new ListViewCellFormattingEventHandler(radListView1_CellFormatting);
        }
     
        void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
        {
            DetailListViewDataCellElement dataCell = e.CellElement as DetailListViewDataCellElement;
     
            if (dataCell != null && dataCell.Data == this.radListView1.Columns[1])
            {
                dataCell.Text = ((DateTime)dataCell.Row[1]).ToShortTimeString();
            }
        }
    }

    I hope this will help you. Feel free to ask if you have any further questions.

    Regards,
    Ivan Todorov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready