如何在datagridview列中转换文件大小。 [英] How to convert file size in datagridview column.

查看:88
本文介绍了如何在datagridview列中转换文件大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在制作Windows应用程序。



在我的申请中,工作流程是这样的



1.在文本框中写入文本,然后单击开始按钮



2. DataGridView中显示的匹配数据



现在我想在Datagridview列中转换为文件大小



例如,在我的数据库中,DBsize值保存在byte foramt中,如下所示:451936256



但很难算数,所以在DataGridView中,我想显示如下转换:431MB



我在下面显示了我的按钮点击代码,我该怎么做?请帮帮我。



谢谢



我尝试了什么:



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:

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();
            }
        }

推荐答案

我做到了.....



首先使用CellFormatting活动



I did it.....

At first using CellFormatting Event

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

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;
                }
            }

        }


这篇关于如何在datagridview列中转换文件大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆