2016-10-13 21 views
1

我正在製作Windows應用程序。如何轉換datagridview列值C#中的文件大小#

在我的應用程序的工作過程就像是在文本框中輸入此

  1. 輸入文字,然後點擊開始按鈕,在DataGridView中

  • 匹配的數據顯示,現在我想轉換爲Datagridview列中的文件大小

    For e xample,在我的數據庫,DBsize值保存在字節foramt像這樣:451936256

    但它`很難指望它,所以在DataGridView的,我想表明其轉換是這樣的:431MB

    我發現我的按鈕點擊代碼下面,我該怎麼做?請幫幫我。

    謝謝

    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) 
           { 
    
            mgrdWebApplication.DataSource = dt; 
           } 
           else 
           { 
            mtlblError.Visible = true; 
           } 
          } 
          catch(Exception ex) 
          { 
           LogWrite.writeLog(ex.ToString(); 
          } 
         } 
    
  • +1

    從[此帖子]添加方法之一(http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using -net)將字節轉換爲可讀格式。然後處理'DataGridView'的'CellFormatting'事件並使用該方法設置'e.Value'。 –

    +0

    謝謝你Reza Aghaei。我明白你說什麼,但這是我的第一個 時間使用它..請你可以解釋它更詳細? –

    +1

    * ['DataGridView.CellFormatting'](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting(v = vs.110).aspx)事件可讓您指示確切的顯示值以及用於單元格顯示的單元格樣式(如背景和前景色)。這意味着您可以處理此事件以進行任何單元格格式化,而不管單元格值本身是否需要格式化。* –

    回答

    1

    我完成了我的代碼,來自Reza Aghaei的建議。

    起初使用CellFormatting事件

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

    和下一步轉換的文件大小的方法。

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

    現在它適用於我的應用程序。

    0

    您可以使用此功能字節轉換爲兆字節。

    public double ConvertBytesToMegabytes(long bytes) 
    { 
        return (bytes/1024f)/1024f; 
    } 
    

    然後在你的代碼上使用該部分。

    webApplicationName = ConvertBytesToMegabytes(dt.Rows[0]["AppName"].ToString()); 
    
    +0

    哦,謝謝你的評論拉爾夫奧拉佐。 –

    +0

    但是,該代碼是我使用另一種方法的參數值。 –

    相關問題