Breaking News

Editors Picks

Friday, December 2, 2011

DataTable manipulation in C#


DataTable dtnewproduct = new DataTable();
dtnewproduct.Columns.Add("ID", typeof(int));
dtnewproduct.Columns.Add("CARD", typeof(string));
dtnewproduct.Columns.Add("QUANTITY", typeof(string));
DataRow dr = dtnewproduct.NewRow();
dr["ID"] = 1;
dr["CARD"] = "SUNIL";
dr["QUANTITY"] = "100";
dtnewproduct.Rows.Add(dr);
dr = dtnewproduct.NewRow();

dr["ID"] = 2;
dr["CARD"] = "SUNIL GURJAR";
dr["QUANTITY"] = "500";
dtnewproduct.Rows.Add(dr);

//For select Some Row Form Datatable
DataRow[] dr = dtnewproduct.Select("CARD='SUNIL'");
DataTable dtNew = new DataTable();
dtNew = dtnewproduct.Clone();
foreach (DataRow drNew in dr)
{
dtNew.ImportRow(drNew);
}
// for Get MAX and MIN Value From DataDatle
double minTotal = Convert.ToDouble(dtnewproduct.Compute("MIN(QUANTITY)", ""));
double maxTotal = Convert.ToDouble(dtnewproduct.Compute("MAX(QUANTITY)", ""));

//For Getting Sum for Values from DataTable
double Total = Convert.ToDouble(dtnewproduct.Compute("SUM(QUANTITY)", "ID = 2"));

//For Getting Avrage for Values from DataTable
double Avg = Convert.ToDouble(dtnewproduct.Compute("AVG(QUANTITY)", "QUANTITY Is Not Null")); }

Available aggregate functions include those shown in the following table.
Avg()
The average of values in a column
Count()
The number of rows (values) in a column
Max()
The largest value in a column
Min()
The smallest value in a column
StDev()
The standard deviation of values in a column
Sum()
The sum of values in a column
Var()
The statistical variance of values in a column

Read more ...

Contact Us

Name

Email *

Message *