dgCategory是用於顯示類別表的DataGrid
自動分頁
AllowPaging= TRUE!!!!
private void dgCategory_PageIndexChanged(object source
System
Web
UI
WebControls
DataGridPageChangedEventArgs e)
{
dgCategory
CurrentPageIndex=e
NewPageIndex
dgCategory
DataBind()
}
排序
默認按
PKId
排序
private void dgCategory_SortCommand(object source
System
Web
UI
WebControls
DataGridSortCommandEventArgs e)
{
string SortOrder=e
SortExpression
ToString()
BindData(SortOrder)
}
private void BindData(string SortOrder)
{
ProductSystem productSys=new ProductSystem()
//底層數據接口
CategoryData categorySet=productSys
GetCategories(
)
//底層數據接口
返回ID為
的Category
DataView categoryView=categorySet
Tables[CategoryData
CATEGORIES_TABLE]
DefaultView
categoryView
Sort=SortOrder
lblTitle
Text=
按
+SortOrder+
排序
dgCategory
DataSource=categoryView
dgCategory
DataBind()
}
private void Page_Load(object sender
System
EventArgs e)
{
BindData(
PKId
)
}
編輯
更新
取消
private void dgCategory_EditCommand(object source
System
Web
UI
WebControls
DataGridCommandEventArgs e)
{
dgCategory
EditItemIndex=e
Item
ItemIndex
BindData(
PKId
)
}
private void dgCategory_CancelCommand(object source
System
Web
UI
WebControls
DataGridCommandEventArgs e)
{
dgCategory
EditItemIndex=
BindData(
PKId
)
}
private void dgCategory_UpdateCommand(object source
System
Web
UI
WebControls
DataGridCommandEventArgs e)
{
string strUpdate=
strUpdate+=
PKId=
+((TextBox)e
Item
Cells[
]
Controls[
])
Text+
strUpdate+=
ParentId=
+((TextBox)e
Item
Cells[
]
Controls[
])
Text+
strUpdate+=
Description=
+((TextBox)e
Item
Cells[
]
Controls[
])
Text+
strUpdate+=
IsLeaf=
+((TextBox)e
Item
Cells[
]
Controls[
])
Text+
try
{
CagegorySet
ExecuteUpdate(strUpdate)
//需要後台提供更新的接口
dgCategory
EditItemIndex=
}
catch
{
Response
Write(
<
script language=
javascript
>alert(
未能完成更新
請…………
)<
/script>
)
}
BindData(
PKId
)
}
private void dgCategory_DeleteCommand(object source
System
Web
UI
WebControls
DataGridCommandEventArgs e)
{
//獲得關鍵字
使用DataKeys集合訪問數據列表控件中每個記錄的鍵值(顯示為一行)
//使得用戶可以存儲鍵字段而無需在控件中顯示它
string PKId=dgCategory
DataKeys[e
Item
ItemIndex]
CategorySet
ExecuteDelete(PKId)
}*/
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27145.html