Thursday, June 17, 2010

DataTable to CSV

private void exportDataTableToCsv(DataTable formattedDataTable, string filename)
{
DataTable toExcel = formattedDataTable.Copy();
HttpContext context = HttpContext.Current;
context.Response.Clear();

foreach (DataColumn column in toExcel.Columns)
{
context.Response.Write(column.ColumnName + ",");
}

context.Response.Write(Environment.NewLine);
foreach (DataRow row in toExcel.Rows)
{
for (int i = 0; i < toExcel.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}

context.Response.Write(Environment.NewLine);
}

context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
context.Response.End();
}

Wednesday, June 2, 2010

Apply Styles for row - GridView

protected void grdTemp_RowDataBound(object sender, GridViewRowEventArgs e)
{

//Apply Styles for Header
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[17].Text == "RepeatHeader")
{
e.Row.BackColor = System.Drawing.Color.FromName("#003366");
e.Row.ForeColor = System.Drawing.Color.White;

}
}

Tuesday, June 1, 2010

Format Currency

double dblAmt;
string curCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat = new System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;

if (dsCash.Tables[0].Rows[0]["Amount"].ToString().Trim() != "")

{
dblAmt = Convert.ToDouble(dsCash.Tables[0].Rows[0]["Amount"].ToString());
lblCashAlternatives.Text = dblAmt.ToString("c", currencyFormat);
}
else
{
lblCashAlternatives.Text = "$0.0";
}