當管理員單擊開始統計按鈕時將觸發Calculate_Click事件其代碼如程序所示
程序 ST_TurnOverStatModuleascxcs
…
private void Calculate_Click(object sender SystemEventArgs e)
{
dg_TurnOverListCurrentPageIndex=;
//從文件WebConfig中讀取連接字符串
string ST_sqldb =
ConfigurationSettingsAppSettings[ConnectionString];
//連接ST_GinShopManage數據庫
SqlConnection ST_Conn = new SqlConnection (ST_sqldb)
//定義SQL語句
String ST_selsql=select hST_BeginTimehST_EndTime
hST_RoomIdcST_NamehST_CNamehST_CIdent
ityIdhST_CPhonehST_TotalPrice+
from ST_History hST_RoomsInfo rST_RoomCategory c where
rST_RoomId=hST_RoomId and rST_RCategoryId=cST_RCategoryId;
//獲取選擇的時間
string datefrom=YearFromListSelectedItemValueToString()
++MouthFromListSelectedItemValueToString()
++DayFromListSelectedItemValueToString()
dateto=YearToListSelectedItem
stringValueToString()
++MouthToListSelectedItemValueToString()
++DayToListSelectedItemValueToString()
ST_selsqlST_selsql=ST_selsql+ and hST_BeginTime between
+datefrom+and+dateto+;
//獲取選擇的方式
if(RCategoryNameListSelectedIndex!=)
{
ST_selsqlST_selsql=ST_selsql+ and
cST_RCategoryId=+RCategoryNameListSelectedIndex+;
}
//創建SqlDataAdapter對象調用ST_selsql
SqlDataAdapter ST_myadapter = new SqlDataAdapter
(ST_selsqlST_Conn)
//創建並填充DataSet
DataSet ds = new DataSet ()
ST_myadapterFill (ds)
dg_TurnOverListDataSource =ds;
dg_TurnOverListDataBind ()
//計算營業額
double SumPrice=;
for(int i=;i<dg_TurnOverListRowsCount;i++){
DataRow dr=dsTables[]Rows[i];
SumPriceSumPrice=SumPrice+ConvertToDouble(
dr[ST_TotalPrice]ToString())}
TurnOverLabelText=SumPriceToString()
//關閉ST_Conn
ST_ConnClose ()
private void ShowAll_Click(object sender SystemEventArgs e)
{
Show_TurnOverList()
}
…
【代碼說明】上述代碼將頁面中的各個控件的值作為條件加入到SQL語句然後進行查詢再將數據顯示出來此外還統計了營業額即對dg_TurnOverList中的金額列的值進行求和將其值用TurnOverLabel的Text屬性顯示出來
注意GridView的每一行都是一個DataRow類
返回目錄ASPNET項目開發指南
編輯推薦
ASPNET MVC 框架揭秘
ASPNET開發寶典
ASP NET開發培訓視頻教程
[] []
From:http://tw.wingwit.com/Article/program/net/201311/15960.html