我正在製作Windows應用程序。如何轉換datagridview列值C#中的文件大小#
在我的應用程序的工作過程就像是在文本框中輸入此
輸入文字,然後點擊開始按鈕,在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();
}
}
從[此帖子]添加方法之一(http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using -net)將字節轉換爲可讀格式。然後處理'DataGridView'的'CellFormatting'事件並使用該方法設置'e.Value'。 –
謝謝你Reza Aghaei。我明白你說什麼,但這是我的第一個 時間使用它..請你可以解釋它更詳細? –
* ['DataGridView.CellFormatting'](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting(v = vs.110).aspx)事件可讓您指示確切的顯示值以及用於單元格顯示的單元格樣式(如背景和前景色)。這意味着您可以處理此事件以進行任何單元格格式化,而不管單元格值本身是否需要格式化。* –