Related articles:
asp.net 2.0 build one of the shopping carts and payment systems
In this article, we will explore the GridView through a simple online store demo program, and begin to analyze a method of generating the datasource of the GridView, and then continue to use that data to create a fully functional shopping interface. Note that the DataSource in this demo program is free to create.
First, Introduction
In the first article, we discussed what a GridView is and how to use it, including how the actual data is bound to it. In this article, we will analyze the source of these data more closely and how to use it with the GridView to implement a simple shopping interface.
Where does the data come from?
Fundamentally, this problem depends on what you are doing. It can come from a static XML file, a dynamic XML feed, a database, and perhaps it is freely created. However, you should ensure that you are satisfied: if there is data, you can ensure that it can "import" into a GridView. In this article, this part of the data is created freely each time the application is restarted.
The datasource used to populate the two GridView is a DataTable. It is built using DataColumns and DataRows. In this main class file there is a function called "Createproductdt", which shows how the DataTable was initially created. Here is the complete implementation of the function:
Private DataTable Createproductdt ()
{
DataTable dtproducts = new DataTable ();
DataColumn productcolumn = new DataColumn ();
Productcolumn.datatype = System.Type.GetType ("System.Int32");
Productcolumn.columnname = "id";
Productcolumn.unique = true;
DtProducts.Columns.Add (Productcolumn);
Productcolumn = new DataColumn ();
Productcolumn.datatype = System.Type.GetType ("System.String");
Productcolumn.columnname = "thumb";
DtProducts.Columns.Add (Productcolumn);
Productcolumn = new DataColumn ();
Productcolumn.datatype = System.Type.GetType ("System.String");
Productcolumn.columnname = "name";
DtProducts.Columns.Add (Productcolumn);
Productcolumn = new DataColumn ();
Productcolumn.datatype = System.Type.GetType ("System.Double");
Productcolumn.columnname = "Price";
DtProducts.Columns.Add (Productcolumn);
Productcolumn = new DataColumn ();
Productcolumn.datatype = SystEm. Type.GetType ("System.Int32");
Productcolumn.columnname = "Quantity";
DtProducts.Columns.Add (Productcolumn);
//make "ID" the primary key
datacolumn[] Pkcolumns = new datacolumn[1];
Pkcolumns[0] = dtproducts.columns["id"];
Dtproducts.primarykey = pkcolumns;
return dtproducts;
}