This Method is used the Reverse the given input DataTable
For Eample,
Input Data Table has the following Records,
NAME TITLE DEV_IN SCOST DCOST SOLD
-------- -------------------- -------- ---------- ---------- ----------
RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4
RAMESH DEAD LEE PASCAL 99.95 4500 73
public static DataTable ReverseDataTable(DataTable dataTable)
{
DataTable table = new DataTable();
DataRow[] rowArray = dataTable.Select();
table = dataTable.Clone();
for (int i = rowArray.Length - 1; i >= 0; i--)
{
table.ImportRow(rowArray[i]);
}
return table;
}
OutPut
NAME TITLE DEV_IN SCOST DCOST SOLD
-------- -------------------- -------- ---------- ---------- ----------
RAMESH DEAD LEE PASCAL 99.95 4500 73
RAMESH HOTEL MANAGEMENT DBASE 12000 35000 4
Thursday, September 24, 2009
Wednesday, September 23, 2009
Filter DataTable and Sort
public static DataTable FilterDataTable(DataTable dataTable, string filterQuery, string sortQuery)
{
DataTable table = new DataTable();
try
{
DataRow[] rowArray = dataTable.Select(filterQuery, sortQuery);
table = dataTable.Clone();
foreach (DataRow row in rowArray)
{
table.ImportRow(row);
}
}
catch
{
table = dataTable.Clone();
}
return table;
}
{
DataTable table = new DataTable();
try
{
DataRow[] rowArray = dataTable.Select(filterQuery, sortQuery);
table = dataTable.Clone();
foreach (DataRow row in rowArray)
{
table.ImportRow(row);
}
}
catch
{
table = dataTable.Clone();
}
return table;
}
Create Excel File
public static bool CreateExcelFile(DataTable dataTable, string fileName)
{
try
{
string str = "";
";
string str3 = "";
string str4 = string.Empty;
StringBuilder builder = new StringBuilder();
foreach (DataColumn column in dataTable.Columns)
{
str3 = str3 + "" + column.ColumnName + " ";
}
str3 = str3 + " ";
foreach (DataRow row in dataTable.Rows)
{
builder.Append("");
for (int i = 0; i < dataTable.Columns.Count; i++)
{
str4 = " ";
if (!string.IsNullOrEmpty(row[i].ToString()))
{
str4 = row[i].ToString();
}
builder.Append("" + str4 + " ");
}
builder.Append(" ");
}
string s = str + str3 + builder.ToString() + str2;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.Write(s);
HttpContext.Current.Response.Flush();
return true;
}
catch
{
return false;
}
}
{
try
{
string str = "
string str3 = "
string str4 = string.Empty;
StringBuilder builder = new StringBuilder();
foreach (DataColumn column in dataTable.Columns)
{
str3 = str3 + "
}
str3 = str3 + "
foreach (DataRow row in dataTable.Rows)
{
builder.Append("
for (int i = 0; i < dataTable.Columns.Count; i++)
{
str4 = " ";
if (!string.IsNullOrEmpty(row[i].ToString()))
{
str4 = row[i].ToString();
}
builder.Append("
}
builder.Append("
}
string s = str + str3 + builder.ToString() + str2;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.Write(s);
HttpContext.Current.Response.Flush();
return true;
}
catch
{
return false;
}
}
Tuesday, September 22, 2009
Build DataTable
public static DataTable BuildDataTable(Dictionary columns)
{
DataTable table = new DataTable();
foreach (KeyValuePair pair in columns)
{
if (!table.Columns.Contains(pair.Key))
{
table.Columns.Add(pair.Key, pair.Value);
}
}
return table;
}
{
DataTable table = new DataTable();
foreach (KeyValuePair
{
if (!table.Columns.Contains(pair.Key))
{
table.Columns.Add(pair.Key, pair.Value);
}
}
return table;
}
Add Row Number to DataTable
public static DataTable AddRowNumber(DataTable dataTable)
{
dataTable.Columns.Add("RowNumber", typeof(int));
for (int i = 0; i < dataTable.Rows.Count; i++)
{
dataTable.Rows[i]["RowNumber"] = i;
}
dataTable.AcceptChanges();
return dataTable;
}
{
dataTable.Columns.Add("RowNumber", typeof(int));
for (int i = 0; i < dataTable.Rows.Count; i++)
{
dataTable.Rows[i]["RowNumber"] = i;
}
dataTable.AcceptChanges();
return dataTable;
}
Set DropDownt Selected Values
public static string SetSelectedValues(DropDownList ddlList, string selectedValues)
{
string selectedValue = string.Empty;
foreach (string selValue in selectedValues.Split(','))
{
ListItem listItem = ddlList.Items.FindByValue(selValue);
if (listItem != null)
{
listItem.Selected = true;
selectedValue += listItem.Value + ",";
}
}
return selectedValue.Trim(',');
}
{
string selectedValue = string.Empty;
foreach (string selValue in selectedValues.Split(','))
{
ListItem listItem = ddlList.Items.FindByValue(selValue);
if (listItem != null)
{
listItem.Selected = true;
selectedValue += listItem.Value + ",";
}
}
return selectedValue.Trim(',');
}
Set CheckBox Selected Values
public static string SetSelectedValues(CheckBoxList chkBoxList, string selectedValues)
{
string selectedValue = string.Empty;
foreach (string selValue in selectedValues.Split(','))
{
ListItem listItem = chkBoxList.Items.FindByValue(selValue);
if (listItem != null)
{
listItem.Selected = true;
selectedValue += listItem.Value + ",";
}
}
return selectedValue.Trim(',');
}
{
string selectedValue = string.Empty;
foreach (string selValue in selectedValues.Split(','))
{
ListItem listItem = chkBoxList.Items.FindByValue(selValue);
if (listItem != null)
{
listItem.Selected = true;
selectedValue += listItem.Value + ",";
}
}
return selectedValue.Trim(',');
}
Subscribe to:
Posts (Atom)