Monday, August 24, 2009

How to Display Sum of each column in the Footer of a GridView Control

Put OnRowDataBound event in Gridview tag
from design page , it will show how to display sum of each column in the footer of the GridView control

protected void gvAssets_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowbal = Convert.ToDecimal(DataBinder.Eval(
e.Row.DataItem, "CurrentBalance"));
balance = balance + rowbal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblbalFooter = (Label)e.Row.FindControl("lblbalFooter");
lblbalFooter.Text = Convert.ToString(String.Format("{0:0,0.00}",
balance));
}
}

Saturday, August 8, 2009

How to Set your screen resolution in .net application [C#]


Sometimes we need change the screen resolution for running our .net desktop application
So here is the code from where we can change the screen resolution with the help of
proc.StartInfo.Arguments = "/x 800 /y 600"; // this will change the screen resolution to
800x600
using System;
using System.Windows.Forms;
using System.Diagnostics;
class Test
{
static void Main()
{
Screen scr = Screen.PrimaryScreen;
int oldWidth = scr.Bounds.Width;
int oldHeight = scr.Bounds.Height;
Process proc = new Process();
proc.StartInfo.FileName = @"c:\qres\ApplicationName.exe"; // put full path in here
proc.StartInfo.Arguments = "/x 800 /y 600"; // say
proc.Start();
proc.WaitForExit();
Console.WriteLine("Press enter to change back to original resolution and exit program");
Console.ReadLine();
proc.StartInfo.Arguments = "/x " + oldWidth.ToString() + "/y "+ oldHeight.ToString();
proc.Start();
proc.WaitForExit();
}
}

Crystal Report in asp.net C#

To show a report

private void Page_Load(object sender, System.EventArgs e)
{
CrystalReport1 report=new CrystalReport1();
CrystalReportViewer1.Visible=true;
DataSet ds=new DataSet("Account");//give same name as on

//dataset1 table header

DataTable table=new DataTable("Account");//give same name as on

//dataset1 table header

table.Columns.Add("Fname",typeof(System.String));
table.Columns.Add("Lname",typeof(System.String));
table.Columns.Add("Salary",typeof(System.String));
DataRow row=table.NewRow();
row["Fname"]="AAAA";
row["Lname"]="BBBB";
row["Salary"]="500";
// add to table

table.Rows.Add(row);
ds.Tables.Add(table);
// set report's dataset

report.SetDataSource(ds);
// set report source

CrystalReportViewer1.ReportSource =report;
}

Thursday, August 6, 2009

String Format for Double in C#

Help link : http://www.csharp-examples.net/string-format-double/

This is a very helpful to me . I have got lot of things from here ..

Few things i can share here also
e.g.
String.Format("{0:0.00}",123.4567); // 123.46 -- just two decimal places
String.Format("{0:0,0.0}",12345.67); // 12,345.7 --Thousands separator

Sql Query for return columns name of a table

SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tempTble'
ORDER BY ORDINAL_POSITION

//Put the tablename as i did 'tempTble'

Wednesday, August 5, 2009

How to Listing Files in a Directory with C#

DirectoryInfo dirInfo = new DirectoryInfo(ryanDataPath + "\\RyansData");
FileInfo[] latestFile = dirInfo.GetFiles("*.csv");

Here latestFile have all the files with .csv extension within RyansData folder

How to Copy a file in C#

Here i am showing how to copy a file source to destination filepath in C#. The file with the filepath 'databasePath' will copy to the destination filepath 'ryanDataPath'
private void CopyDBToCommonFile(string databasePath, string ryanDataPath)
{
FileInfo fi = new FileInfo(ryanDataPath + "\\DB\\SmartChart.mdb"); \\Destination file path
if (!fi.Exists)
{
FileInfo fc = new FileInfo(databasePath); \\Source file path
fc.CopyTo(fi.FullName, true);
}
}