Reputation: 307
I`m making windows application.
In my application working process is like this
Write text in Text Box and click Start button
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
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();
}
}
Upvotes: 1
Views: 731
Reputation: 307
I completed my code, advice from Reza Aghaei.
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 Next Convert 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;
}
}
}
Now it works well in my application.
Upvotes: 1
Reputation: 158
You can use this function to convert bytes to Megabytes.
public double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
Then use that on your code on this part.
webApplicationName = ConvertBytesToMegabytes(dt.Rows[0]["AppName"].ToString());
Upvotes: 0