private void loadDynamicGrid(int selectedYear) { #region Code for preparing the DataTable this.GridView1.Columns.Clear(); //Create an instance of DataTable DataTable dt = new DataTable(); NetProfitDataSource netProfitHandler = new NetProfitDataSource(); Dictionary <string, string> ditMerchantID2Name = new Dictionary <string, string>(); //Create an ID column for adding to the Datatable DataColumn dcol = new DataColumn("Month", typeof(System.String)); dt.Columns.Add(dcol); if (Session["UserID"] != null) { ditMerchantID2Name = getAllMerchantID2NameDictionaryByLoginID(Session["UserID"].ToString()); //Create an ID column for adding to the Datatable foreach (KeyValuePair <string, string> MerchantAccountPair in ditMerchantID2Name) { dcol = new DataColumn(MerchantAccountPair.Key, typeof(System.String)); dt.Columns.Add(dcol); } } dcol = new DataColumn("TOTAL", typeof(System.String)); dt.Columns.Add(dcol); for (int nIndex = 1; nIndex <= 12; nIndex++) { DataRow drow = dt.NewRow(); drow[0] = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(nIndex); foreach (KeyValuePair <string, string> MerchantAccountPair in ditMerchantID2Name) { drow[MerchantAccountPair.Key] = "-"; DataSet ds = netProfitHandler.GetAllSortedYearlyDataByMerchantID("", MerchantAccountPair.Key, selectedYear); if (ds.Tables.Count > 0) { if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { if ((int)row[0] == nIndex) { double dProfit = (double)row[2]; dProfit = Math.Round(dProfit, 2); drow[MerchantAccountPair.Key] = "$" + dProfit.ToString(); } } } } } dt.Rows.Add(drow); } #endregion //calculate the total sum of netprofit foreach (DataRow row in dt.Rows) { double dTotal = 0; for (int i = 0; i < row.ItemArray.Length; i++) { double dNetProfit = 0; if (i == row.ItemArray.Length - 1) { if (dTotal == 0) { row[i] = "-"; } else { row[i] = "$" + dTotal; } } else if (i != 0) { if (row[i] != "-") { string sNetProftString = row[i].ToString(); dNetProfit = double.Parse(sNetProftString.Substring(1)); } dTotal = dTotal + dNetProfit; } } } //Iterate through the columns of the datatable to set the data bound field dynamically. foreach (DataColumn col in dt.Columns) { if (col.ColumnName == "Month") { //Declare the bound field and allocate memory for the bound field. BoundField bfield = new BoundField(); //Initalize the DataField value. bfield.DataField = col.ColumnName; //Initialize the HeaderText field value. bfield.HeaderText = col.ColumnName; //Add the newly created bound field to the GridView. GridView1.Columns.Add(bfield); } else if (col.ColumnName == "TOTAL") { BoundField bfield = new BoundField(); //Initalize the DataField value. bfield.DataField = col.ColumnName; //Initialize the HeaderText field value. bfield.HeaderText = col.ColumnName; //Add the newly created bound field to the GridView. GridView1.Columns.Add(bfield); } else { ButtonField buttonField = new ButtonField(); buttonField.ButtonType = ButtonType.Link; buttonField.DataTextField = col.ColumnName; buttonField.HeaderText = ditMerchantID2Name[col.ColumnName]; GridView1.Columns.Add(buttonField); } } //Initialize the DataSource this.GridView1.DataSource = dt; //Bind the datatable with the GridView. this.GridView1.DataBind(); }
private void GetAccountYearlyNetProfitData(string sMerchantID, int iTargetYear) { NetProfitDataSource netProfitHandler = new NetProfitDataSource(); DataSet ds = netProfitHandler.GetAllSortedYearlyDataByMerchantID("", sMerchantID, iTargetYear); }