Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 144905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:19:42+00:00 2026-05-11T08:19:42+00:00

I have a listview with 4 columns – Name, Extension, Size and Location. I

  • 0

I have a listview with 4 columns – Name, Extension, Size and Location. I have a method that takes the file size in bytes and converts to KB, MB, GB, etc as needed. An example output would be a 1024 byte file which is printed as ‘1KB’. This value is then placed in the listview.

What I need to do is sort the size column intelligently. Right now the sorting is just a simple comparison so 1025 KB is higher than 1024 MB. How can I make it ‘size aware’?

My current sorting code is from this KB article: http://support.microsoft.com/kb/319401 And here is my code that generates the file size text:

        public static string getDynamicFileSize(string fileName)     {         FileInfo fi = new FileInfo(fileName);         long sizeInBytes = fi.Length;         if (sizeInBytes >= 1073741824)         {             double sizeInGB = sizeInBytes / Math.Pow(1024, 3);             return Math.Round(sizeInGB, 2) + ' GB';         }          if (sizeInBytes >= 1048576)         {             double sizeInMB = sizeInBytes / Math.Pow(1024, 2);             return Math.Round(sizeInMB, 2) + ' MB';         }          if (sizeInBytes >= 1024)         {             double sizeInKB = sizeInBytes / Math.Pow(1024,1);             return Math.Round(sizeInKB, 2) + ' KB';         }          //No conversion needed         return sizeInBytes + ' bytes';     } 

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:19:43+00:00Added an answer on May 11, 2026 at 8:19 am

    Most every object in .NET has a Tag member. This is a place where you can stuff anything extra you need to. In your case, I would stuff the file size in bytes into the ListViewSubItem.Tag property. Then, your sort algorithm can use that to sort by instead of the column text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that has two columns, one for a zone name and
Hi i have a listview with diffrent columns ID, Name, Zipcode, City, Country, Phone,
I have a ListView that contains items with four columns. The values in three
I have a ListView with the columns 'Name', 'Expected', 'Total', and I want to
I have a ListView with 3 columns (User Name, Active, and Group) The ItemsSource
I have a ListView (GridView) that I want to sort by 2 columns, so
I have a listview that has 2 columns and a checkbox. What i'm doing
I have a ListView that shows 2 columns per row. I don't have any
I have a listview with two columns and I'm using a context menu to
I have a ListView with a GridView with 3 columns. I want last column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.