Thursday, June 24, 2010

How to create a Dynamic DataTable:

I have a requirement to create a dynamic data table for the shopping cart. When I search in Google I find some coding . I alter that coding change for my requirement.

Here you can create the data type and column name

private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();

DataColumn myDataColumn;

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Productname";
myDataTable.Columns.Add(myDataColumn);



myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Decimal");
myDataColumn.ColumnName = "Quantity";
myDataTable.Columns.Add(myDataColumn);


myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Decimal");
myDataColumn.ColumnName = "Productid";
myDataTable.Columns.Add(myDataColumn);





return myDataTable;
}



private void AddDataToTable(string PrdName, decimal Qty,decimal id, DataTable myTable)
{
DataRow row;

row = myTable.NewRow();


row["Productname"] = PrdName;

row["Quantity"] = Qty;

row["Productid"] = id;


myTable.Rows.Add(row);

}


Remove the duplicates from datatable

private static void RemoveDuplicates(DataTable tbl,

DataColumn[] keyColumns)
{

int rowNdx = 0;

while (rowNdx < tbl.Rows.Count - 1) { DataRow[] dups = FindDups(tbl, rowNdx, keyColumns); if (dups.Length > 0)
{

foreach (DataRow dup in dups)
{

tbl.Rows.Remove(dup);

}

}

else
{

rowNdx++;

}

}

}




private static DataRow[] FindDups(DataTable tbl,

int sourceNdx,

DataColumn[] keyColumns)
{

ArrayList retVal = new ArrayList();



DataRow sourceRow = tbl.Rows[sourceNdx];

for (int i = sourceNdx + 1; i < tbl.Rows.Count; i++) { DataRow targetRow = tbl.Rows[i]; if (IsDup( targetRow,sourceRow, keyColumns)) { retVal.Add(sourceRow); } } return (DataRow[])retVal.ToArray(typeof(DataRow)); } private static bool IsDup(DataRow sourceRow, DataRow targetRow,DataColumn[] keyColumns) { bool retVal = true; foreach (DataColumn column in keyColumns) { retVal = retVal && sourceRow[column].Equals(targetRow[column]); if (!retVal) break; } return retVal; }


/* Here you can pass the values to create a row */

AddDataToTable(strname, txtid, decimal.Parse(strprodid), (DataTable)Session["myDatatable"]);


/* remove the duplicate from datatable */
DataTable Dtcount = new DataTable();

Dtcount = (DataTable)Session["myDatatable"];

DataColumn[] keyColumns = new DataColumn[]
{Dtcount.Columns[2], Dtcount.Columns[2]};

RemoveDuplicates(Dtcount, keyColumns);

Here I can bind the Datatable values in datalist to show the shopping cart.

In that datalist I use text box to update the quantity.

In the txt_TextChanged I write the coding to update the data table.


TextBox tb1 = ((TextBox)(sender));

DataListItem rp1 = ((DataListItem)(tb1.NamingContainer));
string strProductId = rp1.ItemIndex.ToString();

string strUniqeID = datalist1.DataKeys[rp1.ItemIndex].ToString();

/* Here your logic */

Dtcount = (DataTable)Session["myDatatable"];

Dtcount.DefaultView[int.Parse(strProductId)]["Quantity"] = decimal.Parse(strId);

Dtcount.DefaultView[int.Parse(strProductId)]["Price"] = decPrice;

Session["myDatatable"] = Dtcount;

/* Here u can bind the datalist */


/* Delete the particular row in datatable */
/* I write the coding in datalist1_DeleteCommand to delete a particular row */




DataRow dr = ((DataTable)Session["NewDatatable"]).Rows[e.Item.ItemIndex];
dr.Delete();
((DataTable)Session["NewDatatable"]).AcceptChanges();

/* Here u can bind the datalist */

No comments:

Post a Comment