Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I`m making windows application.

In my application working process is like this

1. Write text in Text Box and click Start button

2. Matched data showed in DataGridView

And now I want to convert to file size in Datagridview column

For example, In my database, DBsize value saved in byte foramt like this : 451936256

but It`s hard to count it, So in DataGridView, I want to show convert it like this : 431MB

I showed my button click code below, How can I do that? Please help me.

Thank you

What I have tried:

C#
private void btnStart_Click(object sender, EventArgs e)
        {
            string webApplicationName = string.Empty;
            string siteID = string.Empty;
            mtlblError.Text = "no record returned";

            Migration_Status_DAC dac = new Migration_Status_DAC();
            DataSet ds = new DataSet();

            try
            {
                        
                ds = dac.SelectWebApplicationStatus(mtextUrl.Text);
                DataTable dt = ds.Tables[0];


                if (ds != null && dt != null && dt.Rows.Count > 0)
                {
                    webApplicationName = dt.Rows[0]["AppName"].ToString();
                    mgrdWebApplication.DataSource = dt;
                }
                else
                {
                     mtlblError.Visible = true;
                }
            }
            catch(Exception ex)
            {
                LogWrite.writeLog(ex.ToString();
            }
        }
Posted
Updated 12-Oct-16 16:49pm

1 solution

I did it.....

At first using CellFormatting Event

C#
private void mgrdContentDatabase_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if(this.mgrdContentDatabase.Columns[e.ColumnIndex].HeaderText== "Size(GB)")
            {
                if (e.Value != null)
                {
                    CovertFileSize(e);
                }
            }
        }


and then make a Convert to file size method

C#
private void CovertFileSize(DataGridViewCellFormattingEventArgs formatting)
        {
            if (formatting.Value != null)
            {
                try
                {
                    long bytes;
                    bytes = Convert.ToInt64(formatting.Value);
                    string size = "0 Bytes";

                    //GB
                    if (bytes >= 1073741824.0)
                        size = String.Format("{0:##.##}", bytes / 1073741824.0) + " GB";
                    //MB
                    else if (bytes >= 1048576.0)
                        size = String.Format("{0:##.##}", bytes / 1048576.0) + " MB";
                    //KB
                    else if (bytes >= 1024.0)
                        size = String.Format("{0:##.##}", bytes / 1024.0) + " KB";
                    //Bytes
                    else if (bytes > 0 && bytes < 1024.0)
                        size = bytes.ToString() + " Bytes";

                    formatting.Value = size;
                    formatting.FormattingApplied = true;
                }
                catch(FormatException)
                {
                    formatting.FormattingApplied = false;
                }
            }

        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900